Develop Go Apps
Prerequisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in Quick start.
- installed Go version 1.8+
Install the Go Redis driver
To install the driver, locally run the following go get
command.
$ go get github.com/go-redis/redis
Writing a HelloWorld Redis application
Create a file ybredis_hello_world.go
and copy the contents below.
package main;
import (
"fmt"
"log"
"github.com/go-redis/redis"
)
func main() {
// Connect to the cluster.
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use the default DB
})
defer client.Close()
// Insert some data (for user id 1).
var userid string = "1"
ok, err := client.HMSet(userid, map[string]interface{}{
"name": "John",
"age": "35",
"language": "Redis"}).Result()
if (err != nil) {
log.Fatal(err)
}
fmt.Printf("HMSET returned '%s' for id=%s with values: name=John, age=35, language=Redis\n", ok, userid)
// Query the data.
result, err := client.HGetAll("1").Result()
fmt.Printf("Query result for id=%s: '%v'\n", userid, result)
}
Running the application
To execute the file, run the following command.
$ go run ybredis_hello_world.go
You should see the following as the output.
HMSET returned 'OK' for id=1 with values: name=John, age=35, language=Redis
Query result for id=1: 'map[age:35 language:Redis name:John]'
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .