-
Notifications
You must be signed in to change notification settings - Fork 0
/
or.go
63 lines (53 loc) · 1.43 KB
/
or.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
package cmpop
import (
"cmp"
"github.com/devlights/gomy/output"
)
// Or は、cmp.Or[T comparable]() のサンプルです。
// cmp.Or は、Go 1.22 で追加されました。
//
// > Or returns the first of its arguments that is not equal to the zero value. If no argument is non-zero, it returns the zero value.
//
// > (Orは、引数のうちゼロ値ではない最初の引数を返す。どの引数もゼロ値ではない場合、ゼロ値を返す。)
//
// comparableが対象となるので、以下に対して利用できる。
//
// - booleans
// - numbers
// - strings
// - pointers
// - channels
// - arrays of comparable types
// - structs whose fields are all comparable types
//
// # REFERENCES
// - https://pkg.go.dev/cmp#Or
// - https://pkg.go.dev/builtin#comparable
// - https://blog.carlana.net/post/2024/golang-cmp-or-uses-and-history/
func Or() error {
type (
V struct {
Val int
}
)
var (
ints = []int{0, 0, 99, 1, 0}
strs = []string{"", "hello", "world", ""}
objs = []*V{nil, {999}, nil}
)
output.Stdoutl("[non-zero]", cmp.Or(ints...))
output.Stdoutl("[non-zero]", cmp.Or(strs...))
output.Stdoutl("[non-zero]", cmp.Or(objs...))
return nil
/*
$ task
task: [build] go build .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: cmpop_or
[Name] "cmpop_or"
[non-zero] 99
[non-zero] hello
[non-zero] &{999}
[Elapsed] 53.06µs
*/
}