sorting functions _ golang

Sometimes we‘ll want to sort a collection by something other than its natural order. For example, suppose we wanted to sort strings by their length instead of alphabetically. Here‘s an example of custom sorts in Go

package main

import (
    "fmt"
    "sort"
)

type ByLength []string

func (s ByLength) Len() int {
    return len(s)
}

func (s ByLength) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s ByLength) Less(i, j int) bool {
    return len(s[i]) < len(s[j])
}

func main() {
    fruits := []string{"peach", "banana", "kiwi"}
    sort.Sort(ByLength(fruits))
    fmt.Println(fruits)
}
[kiwi peach banana]

总结 :

  1 : ...

时间: 2024-08-24 08:32:04

sorting functions _ golang的相关文章

functions _ golang

Functions are central in Go. We'll learn about functions with a few different examples package main import ( "fmt" ) func plus(a int, b int) int { return a + b } func plusPlus(a, b, c int) int { return a + b + c } func main() { res := plus(1, 2)

sorting _ golang

Go's sort package implements sorting for builtins and user-defined types. We'll look at sorting for builtins first package main import ( "fmt" "sort" ) func main() { strs := []string{"c", "a", "b"} sort.St

errors _ golang

In Go it's idiomatic to communicate errors via an explicit, separate return value. this constrasts errors via an explicit, separate return value. This constrasts with the exceptions used in languages like Java and Ruby and the overloaded single resul

variadic function _ golang

Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function package main import ( "fmt" ) func sum(nums ...int) { fmt.Println(nums, " ") total := 0 for _, num := range

multiple return values _ golang

Go has built-in support for multiple return values. This feature is used often in idiomatic Go, for example to return both result and error values from a function package main import ( "fmt" ) func vals() (int, int) { return 3, 7 } func main() {

closures _ golang

Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it package main import ( "fmt" ) func intSeq() func() int { i := 0 return func() int { i +=

resursion _ golang

Go support recursive functions. Here's a classic factorial example package main import ( "fmt" ) func fact(n int) int { if n == 0 { return 1 } return n * fact(n-1) } func main() { fmt.Println(fact(7)) } 5040 总结 : 1 : 递归....

if else _ golang

if else 在 golang package main import ( "fmt" ) func main() { if 7%2 == 0 { fmt.Println("7 is even") } else { fmt.Println("7 is odd") } if 8%4 == 0 { fmt.Println("8 is divisible by 4") } if num := 9; num < 0 { fmt

for _ golang

for 是 golang 唯一的 looping 结构, package main import ( "fmt" ) func main() { i := 1 for i <= 3 { fmt.Println(i) i += 1 } for j := 7; j <= 9; j++ { fmt.Println(j) } for { fmt.Println("loop") break } } 1 2 3 7 8 9 loop 总结 : 对于这个, 额, 我觉