-
Notifications
You must be signed in to change notification settings - Fork 574
Go Example (go redis)
Josh Baker edited this page Jan 24, 2022
·
8 revisions
The go-redis package requires a Go version with modules support. So make sure to initialize a Go module:
go mod init github.com/my/repo
And then install go-redis/v8 (note v8 in the import; omitting it is a popular mistake):
go get github.com/go-redis/redis/v8
Tile38 commands must use redis.NewStringCmd(ctx context.Context, args ...interface{})
for query compositions. Then, Process()
to execute the command and Result()
to get the query result.
import (
"context"
"log"
"github.com/go-redis/redis/v8"
)
func main() {
// Tile38 Client Setup
var ctx = context.Background()
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:9851",
})
// SET
setCmd := redis.NewStringCmd(ctx, "SET", "fleet", "truck", "POINT", 33.32, 115.423)
if err := client.Process(ctx, setCmd); err != nil {
log.Fatalf("SET command execution failed: %v", err)
}
setRes, err := setCmd.Result()
if err != nil {
log.Fatalf("Failed to retrieve SET result: %v", err)
}
log.Printf("SET: %s", setRes)
// GET
getCmd := redis.NewStringCmd(ctx, "GET", "fleet", "truck")
if err := client.Process(ctx, getCmd); err != nil {
log.Fatalf("GET command execution failed: %v", err)
}
getRes, err := getCmd.Result()
if err != nil {
log.Fatalf("Failed to retrieve GET result: %v", err)
}
log.Printf("GET: %s", getRes)
}