-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
52 lines (44 loc) · 914 Bytes
/
examples_test.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
49
50
51
52
package slices_test
import (
"fmt"
"github.com/pypaut/slices"
)
func ExampleMap() {
type Person struct {
Name string
Age int
}
persons := []Person{
{Name: "Alice", Age: 31},
{Name: "Bob", Age: 23},
{Name: "Vera", Age: 48},
}
names, _ := slices.Map(
persons, func(p Person) (string, error) { return p.Name, nil },
)
ages, _ := slices.Map(
persons, func(p Person) (int, error) { return p.Age, nil },
)
fmt.Println("Names:", names)
fmt.Println("Ages:", ages)
// Output:
// Names: [Alice Bob Vera]
// Ages: [31 23 48]
}
func ExampleFilter() {
type Person struct {
Name string
Age int
}
persons := []Person{
{Name: "Alice", Age: 31},
{Name: "Bob", Age: 23},
{Name: "Vera", Age: 48},
}
aboveThirty := slices.Filter(
persons, func(p Person) bool { return p.Age > 30 },
)
fmt.Printf("%+v\n", aboveThirty)
// Output:
// [{Name:Alice Age:31} {Name:Vera Age:48}]
}