Develop Go Apps

    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. 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. language varchar)`;
    27. if err := session.Query(createStmt).Exec(); err != nil {
    28. log.Fatal(err)
    29. }
    30. fmt.Println("Created table ybdemo.employee")
    31. // Insert into the table.
    32. var insertStmt string = "INSERT INTO ybdemo.employee(id, name, age, language)" +
    33. " VALUES (1, 'John', 35, 'Go')";
    34. if err := session.Query(insertStmt).Exec(); err != nil {
    35. log.Fatal(err)
    36. }
    37. fmt.Printf("Inserted data: %s\n", insertStmt)
    38. // Read from the table.
    39. var name string
    40. var age int
    41. var language string
    42. iter := session.Query(`SELECT name, age, language FROM ybdemo.employee WHERE id = 1`).Iter()
    43. fmt.Printf("Query for id=1 returned: ");
    44. for iter.Scan(&name, &age, &language) {
    45. fmt.Printf("Row[%s, %d, %s]\n", name, age, language)
    46. }
    47. if err := iter.Close(); err != nil {
    48. log.Fatal(err)
    49. }
    50. }

    Running the app

    To execute the file, run the following command:

    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')
    • installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. If not, please follow these steps in the .
    • installed Go version 1.8+

    Install Go Redis Driver

    To install the driver locally run:

    Create a file ybredis_hello_world.go and copy the contents below.

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

    Running the app

    You should see the following as the output.

    1. Query result for id=1: 'map[age:35 language:Redis name:John]'