Skip to content

Commit 268f7ed

Browse files
authored
Merge pull request #796 from devlights/add-cmp-compare-example
2 parents aebc177 + 0fb0c5c commit 268f7ed

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

examples/basic/cmpop/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
このディレクトリには以下のサンプルがあります。
44

5-
| file | example name | note |
6-
| ----- | ------------ | ----------------------------------------- |
7-
| or.go | cmpop_or | cmp.Or\[T comparable\]\(\) のサンプルです |
5+
| file | example name | note |
6+
| ---------- | ------------- | ---------------------------------------------- |
7+
| or.go | cmpop_or | cmp.Or\[T comparable\]\(\) のサンプルです |
8+
| compare.go | cmpop_compare | cmp.Compare\[T cmp.Orderd\]\(\) のサンプルです |

examples/basic/cmpop/compare.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmpop
2+
3+
import (
4+
"cmp"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// Compare は、cmp.Compare[T cmp.Ordered]() のサンプルです。
10+
//
11+
// 戻り値は、他の言語とルールは同じで
12+
//
13+
// - x が y より小さい場合は -1
14+
// - x と y が同じ場合は   0
15+
// - x が y より大きい場合は 1
16+
//
17+
// となる。比較対象として指定出来るのは cmp.Ordered となっている。
18+
//
19+
// cmp.Or と組合せることにより、ソート処理が書きやすくなる。
20+
//
21+
// # REFERENCES
22+
// - https://pkg.go.dev/cmp@go1.22.2#Compare
23+
// - https://pkg.go.dev/cmp@go1.22.2#Ordered
24+
func Compare() error {
25+
26+
output.Stdoutl("[compare1]", cmp.Compare("hello", "HELLO"))
27+
output.Stdoutl("[compare2]", cmp.Compare(100, 100))
28+
output.Stdoutl("[compare3]", cmp.Compare(99, 100))
29+
30+
return nil
31+
32+
/*
33+
$ task
34+
task: [build] go build .
35+
task: [run] ./try-golang -onetime
36+
37+
ENTER EXAMPLE NAME: cmpop_compare
38+
39+
[Name] "cmpop_compare"
40+
[compare1] 1
41+
[compare2] 0
42+
[compare3] -1
43+
44+
45+
[Elapsed] 42.1µs
46+
*/
47+
48+
}

examples/basic/cmpop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ func NewRegister() mapping.Register {
1414
// Regist -- 登録します.
1515
func (r *register) Regist(m mapping.ExampleMapping) {
1616
m["cmpop_or"] = Or
17+
m["cmpop_compare"] = Compare
1718
}

0 commit comments

Comments
 (0)