diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-01-29 20:52:43 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-01-29 20:52:43 +0000 |
commit | 12ebd6294172cc1108bbadab78fea03e890a6da4 (patch) | |
tree | 4f2fad1f4b778519bdd5941185c7e1d032af055b /libgo/go/bytes/example_test.go | |
parent | 6effa4dc115122a3a4838de0a302dfcadcefeeca (diff) | |
download | gcc-12ebd6294172cc1108bbadab78fea03e890a6da4.tar.gz |
libgo: Update Go library to master revision 15489/921e53d4863c.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@195560 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/bytes/example_test.go')
-rw-r--r-- | libgo/go/bytes/example_test.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libgo/go/bytes/example_test.go b/libgo/go/bytes/example_test.go index 1774a5ab426..dc66b6a40f6 100644 --- a/libgo/go/bytes/example_test.go +++ b/libgo/go/bytes/example_test.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "os" + "sort" ) func ExampleBuffer() { @@ -27,3 +28,41 @@ func ExampleBuffer_reader() { io.Copy(os.Stdout, dec) // Output: Gophers rule! } + +func ExampleCompare() { + // Interpret Compare's result by comparing it to zero. + var a, b []byte + if bytes.Compare(a, b) < 0 { + // a less b + } + if bytes.Compare(a, b) <= 0 { + // a less or equal b + } + if bytes.Compare(a, b) > 0 { + // a greater b + } + if bytes.Compare(a, b) >= 0 { + // a greater or equal b + } + + // Prefer Equal to Compare for equality comparisons. + if bytes.Equal(a, b) { + // a equal b + } + if !bytes.Equal(a, b) { + // a not equal b + } +} + +func ExampleCompare_search() { + // Binary search to find a matching byte slice. + var needle []byte + var haystack [][]byte // Assume sorted + i := sort.Search(len(haystack), func(i int) bool { + // Return haystack[i] >= needle. + return bytes.Compare(haystack[i], needle) >= 0 + }) + if i < len(haystack) && bytes.Equal(haystack[i], needle) { + // Found it! + } +} |