Last active
October 27, 2019 10:22
-
-
Save jiacai2050/4c545b905e504ea5b304561c9d2f16f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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