forked from shadowspore/t38c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.go
71 lines (59 loc) · 1.54 KB
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:build ignore
// +build ignore
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/xjem/t38c"
)
func main() {
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
defer tile38.Close()
// add a couple of points named 'truck1' and 'truck2' to a collection named 'fleet'.
if err := tile38.Keys.Set("fleet", "truck1").Point(33.5123, -112.2693).Do(context.TODO()); err != nil {
log.Fatal(err)
}
if err := tile38.Keys.Set("fleet", "truck2").Point(33.4626, -112.1695).Do(context.TODO()); err != nil {
log.Fatal(err)
}
// search the 'fleet' collection.
// returns both trucks in 'fleet'
scanRes, err := tile38.Search.Scan("fleet").Do(context.TODO())
if err != nil {
log.Fatal(err)
}
printJSON("scan", scanRes)
// search 6 kilometers around a point. returns one truck.
nearbyRes, err := tile38.Search.Nearby("fleet", 33.462, -112.268, 6000).Do(context.TODO())
if err != nil {
log.Fatal(err)
}
printJSON("nearby", nearbyRes)
// key value operations
// returns 'truck1'
truck1, err := tile38.Keys.Get("fleet", "truck1").Object(context.TODO())
if err != nil {
log.Fatal(err)
}
printJSON("get truck1", truck1)
// deletes 'truck2'
if err := tile38.Keys.Del(context.TODO(), "fleet", "truck2"); err != nil {
log.Fatal(err)
}
// removes all
if err := tile38.Keys.Drop(context.TODO(), "fleet"); err != nil {
log.Fatal(err)
}
}
func printJSON(msg string, data interface{}) {
b, _ := json.Marshal(data)
fmt.Printf("%s: %s\n", msg, b)
}