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:
1. $ go get github.com/gocql/gocql
Writing a HelloWorld CQL app
Create a file ybcql_hello_world.go and copy the contents below.
1. package main;
3. import (
4. "fmt"
5. "log"
6. "time"
8. "github.com/gocql/gocql"
9. )
11. func main() {
12. // Connect to the cluster.
13. cluster := gocql.NewCluster("127.0.0.1", "127.0.0.2", "127.0.0.3")
15. // Use the same timeout as the Java driver.
16. cluster.Timeout = 12 * time.Second
18. // Create the session.
19. session, _ := cluster.CreateSession()
20. defer session.Close()
22. // Set up the keyspace and table.
23. if err := session.Query("CREATE KEYSPACE IF NOT EXISTS ybdemo").Exec(); err != nil {
24. log.Fatal(err)
25. }
26. fmt.Println("Created keyspace ybdemo")
29. if err := session.Query(`DROP TABLE IF EXISTS ybdemo.employee`).Exec(); err != nil {
30. log.Fatal(err)
31. }
32. var createStmt = `CREATE TABLE ybdemo.employee (id int PRIMARY KEY,
33. name varchar,
34. age int,
35. language varchar)`;
36. if err := session.Query(createStmt).Exec(); err != nil {
37. log.Fatal(err)
38. }
39. fmt.Println("Created table ybdemo.employee")
41. // Insert into the table.
42. var insertStmt string = "INSERT INTO ybdemo.employee(id, name, age, language)" +
43. " VALUES (1, 'John', 35, 'Go')";
44. if err := session.Query(insertStmt).Exec(); err != nil {
45. log.Fatal(err)
46. }
47. fmt.Printf("Inserted data: %s\n", insertStmt)
49. // Read from the table.
50. var name string
51. var age int
52. var language string
53. iter := session.Query(`SELECT name, age, language FROM ybdemo.employee WHERE id = 1`).Iter()
54. fmt.Printf("Query for id=1 returned: ");
55. for iter.Scan(&name, &age, &language) {
56. fmt.Printf("Row[%s, %d, %s]\n", name, age, language)
57. }
59. if err := iter.Close(); err != nil {
60. log.Fatal(err)
61. }
62. }
Running the app
To execute the file, run the following command:
1. $ go run ybcql_hello_world.go
You should see the following as the output.
1. Created keyspace ybdemo
2. Created table ybdemo.employee
3. Inserted data: INSERT INTO ybdemo.employee(id, name, age, language) VALUES (1, 'John', 35, 'Go')
4. 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:
1. $ go get github.com/go-redis/redis
Writing a HelloWorld Redis app
Create a file ybredis_hello_world.go and copy the contents below.
1. package main;
3. import (
4. "fmt"
5. "log"
7. "github.com/go-redis/redis"
8. )
10. func main() {
11. // Connect to the cluster.
12. client := redis.NewClient(&redis.Options{
13. Addr: "localhost:6379",
14. Password: "", // no password set
15. DB: 0, // use the default DB
16. })
17. defer client.Close()
19. // Insert some data (for user id 1).
20. var userid string = "1"
21. ok, err := client.HMSet(userid, map[string]interface{}{
22. "name": "John",
23. "age": "35",
24. "language": "Redis"}).Result()
26. if (err != nil) {
27. log.Fatal(err)
28. }
29. fmt.Printf("HMSET returned '%s' for id=%s with values: name=John, age=35, language=Redis\n", ok, userid)
31. // Query the data.
32. result, err := client.HGetAll("1").Result()
33. fmt.Printf("Query result for id=%s: '%v'\n", userid, result)
34. }
Running the app
To execute the file, run the following command:
1. $ go run ybredis_hello_world.go
You should see the following as the output.
1. HMSET returned 'OK' for id=1 with values: name=John, age=35, language=Redis
2. Query result for id=1: 'map[age:35 language:Redis name:John]'
