Skip to content

Commit 5359d90

Browse files
committed
Add examples/basic/cmpop/or.go
1 parent 9480318 commit 5359d90

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

examples/basic/cmpop/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
| file | example name | note |
6+
| ----- | ------------ | ----------------------------------------- |
7+
| or.go | cmpop_or | cmp.Or\[T comparable\]\(\) のサンプルです |

examples/basic/cmpop/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package cmpop -- cmpパッケージのサンプルが配置されています。
3+
*/
4+
package cmpop

examples/basic/cmpop/examples.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmpop
2+
3+
import "github.com/devlights/try-golang/mapping"
4+
5+
type (
6+
register struct{}
7+
)
8+
9+
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。
10+
func NewRegister() mapping.Register {
11+
return new(register)
12+
}
13+
14+
// Regist -- 登録します.
15+
func (r *register) Regist(m mapping.ExampleMapping) {
16+
m["cmpop_or"] = Or
17+
}

examples/basic/cmpop/or.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmpop
2+
3+
import (
4+
"cmp"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// Or は、cmp.Or[T comparable]() のサンプルです。
10+
// cmp.Or は、Go 1.22 で追加されました。
11+
//
12+
// > 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.
13+
//
14+
// > (Orは、引数のうちゼロ値ではない最初の引数を返す。どの引数もゼロ値ではない場合、ゼロ値を返す。)
15+
//
16+
// comparableが対象となるので、以下に対して利用できる。
17+
//
18+
// - booleans
19+
// - numbers
20+
// - strings
21+
// - pointers
22+
// - channels
23+
// - arrays of comparable types
24+
// - structs whose fields are all comparable types
25+
//
26+
// # REFERENCES
27+
// - https://pkg.go.dev/cmp#Or
28+
// - https://pkg.go.dev/builtin#comparable
29+
func Or() error {
30+
type (
31+
V struct {
32+
Val int
33+
}
34+
)
35+
var (
36+
ints = []int{0, 0, 99, 1, 0}
37+
strs = []string{"", "hello", "world", ""}
38+
objs = []*V{nil, {999}, nil}
39+
)
40+
41+
output.Stdoutl("[non-zero]", cmp.Or(ints...))
42+
output.Stdoutl("[non-zero]", cmp.Or(strs...))
43+
output.Stdoutl("[non-zero]", cmp.Or(objs...))
44+
45+
return nil
46+
47+
/*
48+
$ task
49+
task: [build] go build .
50+
task: [run] ./try-golang -onetime
51+
52+
ENTER EXAMPLE NAME: cmpop_or
53+
54+
[Name] "cmpop_or"
55+
[non-zero] 99
56+
[non-zero] hello
57+
[non-zero] &{999}
58+
59+
60+
[Elapsed] 53.06µs
61+
*/
62+
}

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/devlights/try-golang/examples/basic/builtins"
1010
"github.com/devlights/try-golang/examples/basic/byteop"
1111
"github.com/devlights/try-golang/examples/basic/cmdexec"
12+
"github.com/devlights/try-golang/examples/basic/cmpop"
1213
"github.com/devlights/try-golang/examples/basic/comments"
1314
"github.com/devlights/try-golang/examples/basic/constants"
1415
"github.com/devlights/try-golang/examples/basic/containers"
@@ -95,6 +96,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
9596
bufferop.NewRegister().Regist(m)
9697
byteop.NewRegister().Regist(m)
9798
cmdexec.NewRegister().Regist(m)
99+
cmpop.NewRegister().Regist(m)
98100
comments.NewRegister().Regist(m)
99101
constants.NewRegister().Regist(m)
100102
containers.NewRegister().Regist(m)

0 commit comments

Comments
 (0)