-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
46 lines (40 loc) · 1.02 KB
/
example_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
package as_test
import (
"fmt"
"github.com/lunemec/as"
)
func Example() {
for _, n := range []int{127, 128} {
num, err := as.Int8(n)
if err != nil {
fmt.Printf("Input invalid: %d, err: %s\n", num, err)
} else {
fmt.Printf("Input valid: %d\n", num)
}
}
// Output: Input valid: 127
// Input invalid: -128, err: 128 (int) overflows int8
}
func ExampleT() {
for _, n := range []int{127, 128} {
num, err := as.T[int8](n)
if err != nil {
fmt.Printf("Input invalid: %d, err: %s\n", num, err)
} else {
fmt.Printf("Input valid: %d\n", num)
}
}
// Output: Input valid: 127
// Input invalid: -128, err: 128 (int) overflows int8
}
func ExampleSliceT() {
out, err := as.SliceT[int, int8]([]int{127, 128})
fmt.Printf("Output: %+v, error: %+v\n", out, err)
// Output: Output: [127 0], error: 1 error occurred:
// * at index [1]: 128 (int) overflows int8
}
func ExampleSliceTUnchecked() {
out := as.SliceTUnchecked[int, int8]([]int{127, 128})
fmt.Printf("Output: %+v\n", out)
// Output: Output: [127 -128]
}