-
Notifications
You must be signed in to change notification settings - Fork 0
/
function04.go
49 lines (41 loc) · 1.73 KB
/
function04.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Alta3 Research | Go Essentials
Function literals - Anonymous */
package main
import (
"fmt"
"sort"
)
func main() {
// people is a slice of structures
// we are defining this "anonymously", which means to
// instantiate the instance immediately after declaring the type
people := []struct {
Host string
Mac string
Ram int
Cpu int
}{
{"merlin", "2e549138a9e3", 1024, 1},
{"prospero", "3c539188c9e3", 2048, 2},
{"nestor", "1b166127a9e3", 2048, 1},
{"odin", "4d545b88c9e3", 4096, 2},
}
// this technique makes sense if you're going to use a struct once and be done
// marshaling or unmarshalling json, or prepping HTTP responses
// In the following, if i is 0, j is 1 (i is the index before j)
// The less function is a comparator function that reports whether the element at index i
// should sort before the element at index j. If both less(i, j) and less(j, i) are false,
// then the elements at index i and j are considered equal.
//
// An anonymous function is a function which doesn’t contain any name.
// It is useful when you want to create an inline function.
// sort in ascending order
sort.Slice(people, func(i, j int) bool { return people[i].Host < people[j].Host })
fmt.Println("By Hostname:", people)
sort.Slice(people, func(i, j int) bool { return people[i].Ram < people[j].Ram })
fmt.Println("By RAM:", people)
// assigning a nameless function to a variable is also possible
algo := func(i, j int) bool { return people[i].Mac > people[j].Mac }
sort.Slice(people, algo)
fmt.Println("Reverse sort by MAC:", people)
}