Skip to content

Instantly share code, notes, and snippets.

@norbinsh
Created July 17, 2020 11:12
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 norbinsh/41a39dc69f9f36adf6cfd9b8ee500470 to your computer and use it in GitHub Desktop.
Save norbinsh/41a39dc69f9f36adf6cfd9b8ee500470 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/sha256"
"encoding/binary"
"fmt"
)
func main() {
servers := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12", "10.10.10.10.13"}
for _, v := range servers {
hash := consistentHashing(v)
fmt.Println(hash % uint64(len(servers)))
}
}
func consistentHashing(s string) uint64 {
h := sha256.New()
h.Write([]byte(s))
data := binary.BigEndian.Uint64(h.Sum(nil))
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment