Golang bytes.Compare() function example

bytes

Compare returns an integer comparing two byte slices lexicographically. See reference (1).

Golang bytes.Compare() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 fmt.Println(bytes.Compare([]byte{1}, []byte{2}))
 fmt.Println(bytes.Compare([]byte{1}, []byte{1, 2}))
 fmt.Println(bytes.Compare([]byte{1}, []byte{1}))
 fmt.Println(bytes.Compare([]byte{1}, []byte{0}))
 fmt.Println(bytes.Compare([]byte{2}, []byte{1, 2}))
 }

Output :

-1

-1

0

1

1

Reference :

1 http://en.wikipedia.org/wiki/Lexicographical_order

http://golang.org/pkg/bytes/#Compare

Advertisement