-
-
Save ardyantohermawan/ecdb9aa2c70a828548e055c667696999 to your computer and use it in GitHub Desktop.
Getting Started with Apace Cassandra and Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/gocql/gocql" | |
) | |
func main() { | |
// connect to the cluster | |
cluster := gocql.NewCluster("127.0.0.1") | |
cluster.Keyspace = "demo" | |
session, _ := cluster.CreateSession() | |
defer session.Close() | |
// insert a user | |
if err := session.Query("INSERT INTO users (lastname, age, city, email, firstname) VALUES ('Jones', 35, 'Austin', '[email protected]', 'Bob')").Exec(); err != nil { | |
log.Fatal(err) | |
} | |
// Use select to get the user we just entered | |
var firstname, lastname, city, email string | |
var age int | |
if err := session.Query("SELECT firstname, age FROM users WHERE lastname='Jones'").Scan(&firstname, &age); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(firstname, age) | |
// Update the same user with a new age | |
if err := session.Query("UPDATE users SET age = 36 WHERE lastname = 'Jones'").Exec(); err != nil { | |
log.Fatal(err) | |
} | |
// Select and show the change | |
iter := session.Query("SELECT firstname, age FROM users WHERE lastname='Jones'").Iter() | |
for iter.Scan(&firstname, &age) { | |
fmt.Println(firstname, age) | |
} | |
if err := iter.Close(); err != nil { | |
log.Fatal(err) | |
} | |
// Delete the user from the users table | |
if err := session.Query("DELETE FROM users WHERE lastname = 'Jones'").Exec(); err != nil { | |
log.Fatal(err) | |
} | |
// Show that the user is gone | |
session.Query("SELECT * FROM users").Iter() | |
for iter.Scan(&lastname, &age, &city, &email, &firstname) { | |
fmt.Println(lastname, age, city, email, firstname) | |
} | |
if err := iter.Close(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment