Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Last active December 19, 2015 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chespinoza/5980032 to your computer and use it in GitHub Desktop.
Save chespinoza/5980032 to your computer and use it in GitHub Desktop.
A example of string to a slice of bytes in Go, using a function example from Effective Go
// From effective Go
// Compare returns an integer comparing the two byte slices,
// lexicographically.
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b
package main
import "fmt"
func Compare(a, b []byte) int {
for i := 0; i < len(a) && i < len(b); i++ {
switch {
case a[i] > b[i]:
return 1
case a[i] < b[i]:
return -1
}
}
switch {
case len(a) < len(b):
return -1
case len(a) > len(b):
return 1
}
return 0
}
func main() {
a := []byte("chaus")
b := []byte("chauss")
fmt.Printf("'%s' es igual a '%s'? = %d", a, b, Compare(a, b))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment