https://github.com/yguilai/go-consistenthash
a consistent hash implementation in go
https://github.com/yguilai/go-consistenthash
consistent-hash consistenthash generic generic-types hash-ring
Last synced: about 1 month ago
JSON representation
a consistent hash implementation in go
- Host: GitHub
- URL: https://github.com/yguilai/go-consistenthash
- Owner: yguilai
- License: mit
- Created: 2025-05-19T02:56:57.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-05-21T02:37:14.000Z (9 months ago)
- Last Synced: 2025-06-21T21:02:49.677Z (8 months ago)
- Topics: consistent-hash, consistenthash, generic, generic-types, hash-ring
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-consistenthash
a consistent hash implementation that inspired by [go-zero](https://github.com/zeromicro/go-zero)
this library is without third dependency, the go sdk should be since 1.18, because of this library use generic feature
## Usage
```bash
go get github.com/yguilai/go-consistenthash
```
### Simple Use Case
```go
package main
import (
"fmt"
consistenthash "github.com/yguilai/go-consistenthash"
)
type StringNode string
// implement consistenthash.Node interface
func (s StringNode) String() string {
return string(s)
}
func main() {
// use default options
ch := consistenthash.New[StringNode]()
nodes := []StringNode{"localhost:1", "localhost:2", "localhost:3"}
ch.Add(nodes...)
node, ok := ch.Get("key1")
fmt.Printf("node: %v, ok: %v", node, ok)
}
```