A radix-tree based router. No-allocation lookups for extra speed!
https://git.akyoto.dev/go/router
tests | ||
.gitignore | ||
Flow.go | ||
go.mod | ||
LICENSE | ||
Parameter.go | ||
README.md | ||
Router.go | ||
Tree.go | ||
TreeNode.go |
Router
A radix-tree based no-allocation router in Go. All credit to Eduard Urbach for his incredible work.
Features
- Efficient lookup
- Generic data structure
- Zero dependencies
Installation
go get git.sharkk.net/Go/Router
Usage
router := router.New[string]()
// Static routes
router.Add("GET", "/hello", "...")
router.Add("GET", "/world", "...")
// Parameter routes
router.Add("GET", "/users/:id", "...")
router.Add("GET", "/users/:id/comments", "...")
// Wildcard routes
router.Add("GET", "/images/*path", "...")
// Simple lookup
data, params := router.Lookup("GET", "/users/42")
fmt.Println(data, params)
// Efficient lookup
data := router.LookupNoAlloc("GET", "/users/42", func(key string, value string) {
fmt.Println(key, value)
})
Benchmarks
goos: linux
goarch: amd64
pkg: git.sharkk.net/Go/Router/tests
cpu: AMD Ryzen 9 7950X 16-Core Processor
BenchmarkBlog/Len1-Param0-32 384992013 3.337 ns/op 0 B/op 0 allocs/op
BenchmarkBlog/Len1-Param1-32 199599014 6.021 ns/op 0 B/op 0 allocs/op
BenchmarkGithub/Len7-Param0-32 256332994 4.678 ns/op 0 B/op 0 allocs/op
BenchmarkGithub/Len7-Param1-32 269038417 4.455 ns/op 0 B/op 0 allocs/op
BenchmarkGithub/Len7-Param2-32 256228226 4.673 ns/op 0 B/op 0 allocs/op
PASS
ok git.sharkk.net/Go/Router/tests 8.410s