-
Notifications
You must be signed in to change notification settings - Fork 4
/
slice_examples_test.go
80 lines (65 loc) · 1.4 KB
/
slice_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package set_test
import (
"fmt"
"reflect"
"github.com/nofeaturesonlybugs/set"
)
func ExampleSlice() {
var v set.Value
var nums []int
var numsppp ***[]int
slice, err := set.Slice(&nums)
if err != nil {
fmt.Println(err)
return
}
for k, elem := range []interface{}{"42", false, "true", "3.14"} {
if k == 0 {
v = set.V(slice.Elem())
} else {
v.Rebind(slice.Elem())
}
if err = v.To(elem); err != nil {
fmt.Println(err)
return
}
slice.Append(v.TopValue)
}
slice, err = set.Slice(&numsppp)
if err != nil {
fmt.Println(err)
return
}
for k, elem := range []interface{}{"42", false, "true", "3.14"} {
if k == 0 {
v = set.V(slice.Elem())
} else {
v.Rebind(slice.Elem())
}
if err = v.To(elem); err != nil {
fmt.Println(err)
return
}
slice.Append(v.TopValue)
}
fmt.Println(nums)
fmt.Println(***numsppp)
// Output: [42 0 1 3]
// [42 0 1 3]
}
func ExampleSlice_errors() {
var n int
var numspp **[]int
_, err := set.Slice(n)
fmt.Println(err)
_, err = set.Slice(&n)
fmt.Println(err)
_, err = set.Slice(reflect.ValueOf(&n))
fmt.Println(err)
_, err = set.Slice(numspp)
fmt.Println(err)
// Output: set: Slice: invalid slice: expected pointer to slice; got int
// set: Slice: invalid slice: expected pointer to slice; got *int
// set: Slice: invalid slice: expected pointer to slice; got *int
// set: Slice: read only value: can not set **[]int
}