Develop Go Apps
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. If not, please follow these steps in the quick start guide.
- installed Go version 1.8+
Install Go Cassandra Driver
To install the driver locally run:
$ go get github.com/gocql/gocql
Writing a HelloWorld CQL app
Create a file ybcql_hello_world.go
and copy the contents below.
package main;
import (
"fmt"
"log"
"time"
"github.com/gocql/gocql"
)
func main() {
// Connect to the cluster.
cluster := gocql.NewCluster("127.0.0.1", "127.0.0.2", "127.0.0.3")
// Use the same timeout as the Java driver.
cluster.Timeout = 12 * time.Second
// Create the session.
session, _ := cluster.CreateSession()
defer session.Close()
// Set up the keyspace and table.
if err := session.Query("CREATE KEYSPACE IF NOT EXISTS ybdemo").Exec(); err != nil {
log.Fatal(err)
}
fmt.Println("Created keyspace ybdemo")
if err := session.Query(`DROP TABLE IF EXISTS ybdemo.employee`).Exec(); err != nil {
log.Fatal(err)
}
var createStmt = `CREATE TABLE ybdemo.employee (id int PRIMARY KEY,
name varchar,
age int,
language varchar)`;
if err := session.Query(createStmt).Exec(); err != nil {
log.Fatal(err)
}
fmt.Println("Created table ybdemo.employee")
// Insert into the table.
var insertStmt string = "INSERT INTO ybdemo.employee(id, name, age, language)" +
" VALUES (1, 'John', 35, 'Go')";
if err := session.Query(insertStmt).Exec(); err != nil {
log.Fatal(err)
}
fmt.Printf("Inserted data: %s\n", insertStmt)
// Read from the table.
var name string
var age int
var language string
iter := session.Query(`SELECT name, age, language FROM ybdemo.employee WHERE id = 1`).Iter()
fmt.Printf("Query for id=1 returned: ");
for iter.Scan(&name, &age, &language) {
fmt.Printf("Row[%s, %d, %s]\n", name, age, language)
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
Running the app
To execute the file, run the following command:
$ go run ybcql_hello_world.go
You should see the following as the output.
Created keyspace ybdemo
Created table ybdemo.employee
Inserted data: INSERT INTO ybdemo.employee(id, name, age, language) VALUES (1, 'John', 35, 'Go')
Query for id=1 returned: Row[John, 35, Go]
Pre-requisites
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 the quick start guide.
- installed Go version 1.8+
Install Go Redis Driver
To install the driver locally run:
$ go get github.com/go-redis/redis
Writing a HelloWorld Redis app
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 app
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 .