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;
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "github.com/gocql/gocql"
  7. )
  8. func main() {
  9. // Connect to the cluster.
  10. cluster := gocql.NewCluster("127.0.0.1", "127.0.0.2", "127.0.0.3")
  11. // Use the same timeout as the Java driver.
  12. cluster.Timeout = 12 * time.Second
  13. // Create the session.
  14. session, _ := cluster.CreateSession()
  15. defer session.Close()
  16. // Set up the keyspace and table.
  17. if err := session.Query("CREATE KEYSPACE IF NOT EXISTS ybdemo").Exec(); err != nil {
  18. log.Fatal(err)
  19. }
  20. fmt.Println("Created keyspace ybdemo")
  21. if err := session.Query(`DROP TABLE IF EXISTS ybdemo.employee`).Exec(); err != nil {
  22. log.Fatal(err)
  23. }
  24. var createStmt = `CREATE TABLE ybdemo.employee (id int PRIMARY KEY,
  25. name varchar,
  26. age int,
  27. language varchar)`;
  28. if err := session.Query(createStmt).Exec(); err != nil {
  29. log.Fatal(err)
  30. }
  31. fmt.Println("Created table ybdemo.employee")
  32. // Insert into the table.
  33. var insertStmt string = "INSERT INTO ybdemo.employee(id, name, age, language)" +
  34. " VALUES (1, 'John', 35, 'Go')";
  35. if err := session.Query(insertStmt).Exec(); err != nil {
  36. log.Fatal(err)
  37. }
  38. fmt.Printf("Inserted data: %s\n", insertStmt)
  39. // Read from the table.
  40. var name string
  41. var age int
  42. var language string
  43. iter := session.Query(`SELECT name, age, language FROM ybdemo.employee WHERE id = 1`).Iter()
  44. fmt.Printf("Query for id=1 returned: ");
  45. for iter.Scan(&name, &age, &language) {
  46. fmt.Printf("Row[%s, %d, %s]\n", name, age, language)
  47. }
  48. if err := iter.Close(); err != nil {
  49. log.Fatal(err)
  50. }
  51. }

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;
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/go-redis/redis"
  6. )
  7. func main() {
  8. // Connect to the cluster.
  9. client := redis.NewClient(&redis.Options{
  10. Addr: "localhost:6379",
  11. Password: "", // no password set
  12. DB: 0, // use the default DB
  13. })
  14. defer client.Close()
  15. // Insert some data (for user id 1).
  16. var userid string = "1"
  17. ok, err := client.HMSet(userid, map[string]interface{}{
  18. "name": "John",
  19. "age": "35",
  20. "language": "Redis"}).Result()
  21. if (err != nil) {
  22. log.Fatal(err)
  23. }
  24. fmt.Printf("HMSET returned '%s' for id=%s with values: name=John, age=35, language=Redis\n", ok, userid)
  25. // Query the data.
  26. result, err := client.HGetAll("1").Result()
  27. fmt.Printf("Query result for id=%s: '%v'\n", userid, result)
  28. }

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]'