From ddc8591a7550db2f662a375e311afe388a147320 Mon Sep 17 00:00:00 2001 From: mmadfox Date: Wed, 29 Dec 2021 16:49:46 +0300 Subject: [PATCH] add example --- examples/sample/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/sample/main.go diff --git a/examples/sample/main.go b/examples/sample/main.go new file mode 100644 index 0000000..add8e5e --- /dev/null +++ b/examples/sample/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + + h3geodist "github.com/mmadfox/go-h3geo-dist" + "github.com/uber/h3-go/v3" +) + +func main() { + h3dist, err := h3geodist.New(h3geodist.Level5, + h3geodist.WithVNodes(8), + ) + if err != nil { + panic(err) + } + + _ = h3dist.Add("127.0.0.1") + _ = h3dist.Add("127.0.0.2") + + cells, err := h3.HexRange(h3.FromString("854176affffffff"), 8) + if err != nil { + panic(err) + } + + stats := make(map[string]int) + for _, cell := range cells { + level := h3.Resolution(cell) + dcell, ok := h3dist.Lookup(cell) + if ok { + fmt.Printf("cell=%v, level=%d, host=%s\n", dcell.H3ID, level, dcell.Host) + stats[dcell.Host]++ + } + } + + fmt.Println("Stats:") + for host, counter := range stats { + fmt.Printf("- host=%s, counter=%d\n", host, counter) + } +}