Skip to content

Instantly share code, notes, and snippets.

@jiacai2050
Last active October 27, 2019 10:22
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 jiacai2050/4c545b905e504ea5b304561c9d2f16f8 to your computer and use it in GitHub Desktop.
Save jiacai2050/4c545b905e504ea5b304561c9d2f16f8 to your computer and use it in GitHub Desktop.
import (
"encoding/binary"
"github.com/spaolacci/murmur3"
)
func UnsafeToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
func GetID(v string) int {
h64 := murmur3.New64()
rawBs := UnsafeToBytes(v)
bs := make([]byte, len(rawBs))
copy(bs, rawBs)
if _, err := h64.Write(bs); err != nil {
return -1
}
return int(binary.BigEndian.Uint64(h64.Sum(nil))) % 10
}
// GetID will throw panic several times in one day in my server's log
// PANIC: runtime error: slice bounds out of range [:9] with capacity 2
// .../vendor/github.com/spaolacci/murmur3/murmur.go:42 +0x378
// It make no sense panic here because there is bound check in Write
// https://github.com/spaolacci/murmur3/blob/master/murmur.go#L42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment