Skip to content

Commit

Permalink
Merge pull request #796 from devlights/add-cmp-compare-example
Browse files Browse the repository at this point in the history
  • Loading branch information
devlights authored Apr 30, 2024
2 parents aebc177 + 0fb0c5c commit 268f7ed
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/basic/cmpop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

| file | example name | note |
| ----- | ------------ | ----------------------------------------- |
| or.go | cmpop_or | cmp.Or\[T comparable\]\(\) のサンプルです |
| file | example name | note |
| ---------- | ------------- | ---------------------------------------------- |
| or.go | cmpop_or | cmp.Or\[T comparable\]\(\) のサンプルです |
| compare.go | cmpop_compare | cmp.Compare\[T cmp.Orderd\]\(\) のサンプルです |
48 changes: 48 additions & 0 deletions examples/basic/cmpop/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmpop

import (
"cmp"

"github.com/devlights/gomy/output"
)

// Compare は、cmp.Compare[T cmp.Ordered]() のサンプルです。
//
// 戻り値は、他の言語とルールは同じで
//
// - x が y より小さい場合は -1
// - x と y が同じ場合は   0
// - x が y より大きい場合は 1
//
// となる。比較対象として指定出来るのは cmp.Ordered となっている。
//
// cmp.Or と組合せることにより、ソート処理が書きやすくなる。
//
// # REFERENCES
// - https://pkg.go.dev/[email protected]#Compare
// - https://pkg.go.dev/[email protected]#Ordered
func Compare() error {

output.Stdoutl("[compare1]", cmp.Compare("hello", "HELLO"))
output.Stdoutl("[compare2]", cmp.Compare(100, 100))
output.Stdoutl("[compare3]", cmp.Compare(99, 100))

return nil

/*
$ task
task: [build] go build .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: cmpop_compare
[Name] "cmpop_compare"
[compare1] 1
[compare2] 0
[compare3] -1
[Elapsed] 42.1µs
*/

}
1 change: 1 addition & 0 deletions examples/basic/cmpop/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ func NewRegister() mapping.Register {
// Regist -- 登録します.
func (r *register) Regist(m mapping.ExampleMapping) {
m["cmpop_or"] = Or
m["cmpop_compare"] = Compare
}

0 comments on commit 268f7ed

Please sign in to comment.