Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating a Go Application
- Creating a GCP Spanner Database
- Connecting to the Database
- Performing CRUD Operations
- Conclusion
Introduction
In this tutorial, we will learn how to build a Go application for managing the Google Cloud Spanner database. Google Cloud Spanner is a fully managed, relational database service that offers scalability, consistency, and global distribution. By the end of this tutorial, you will be able to create a Go application that can connect to a GCP Spanner database, perform CRUD operations, and effectively manage your data.
Prerequisites
To follow along with this tutorial, you will need:
- Basic knowledge of Go programming language
- A Google Cloud Platform (GCP) account
- GCP project with billing enabled
- The GCP SDK (Software Development Kit) installed on your machine
- Go installed on your machine
Setup
-
Install the GCP SDK by following the instructions provided here.
-
Install Go by following the official installation guide for your operating system, which can be found here.
Creating a Go Application
-
Create a new directory for your Go application:
```bash mkdir gcp-spanner-app cd gcp-spanner-app ```
-
Initialize a new Go module:
```bash go mod init github.com/your-username/gcp-spanner-app ```
-
Install the required Go packages:
```bash go get cloud.google.com/go/spanner ```
Creating a GCP Spanner Database
-
Create a new project in your Google Cloud Console if you haven’t already. Make note of the project ID.
-
Enable the Spanner API by visiting the API & Services Dashboard.
-
Create a new Spanner instance by running the following command:
```bash gcloud spanner instances create spanner-instance --config=regional-us-central1 ```
-
Create a new database by running the following command:
```bash gcloud spanner databases create spanner-database --instance=spanner-instance --ddl="CREATE TABLE Users (Id INT64, Name STRING(100))" ```
Connecting to the Database
-
Import the required packages in your Go application:
```go import ( "cloud.google.com/go/spanner" "context" ) ```
-
Initialize a new Spanner client and connect to the database:
```go ctx := context.Background() client, err := spanner.NewClient(ctx, "projects/<project-id>/instances/spanner-instance/databases/spanner-database") if err != nil { log.Fatal(err) } defer client.Close() ```
Performing CRUD Operations
-
Inserting data into the database:
```go _, err := client.Apply(ctx, []*spanner.Mutation{ spanner.Insert("Users", []string{"Id", "Name"}, []interface{}{1, "John Doe"}, ), }) if err != nil { log.Fatal(err) } ```
-
Querying data from the database:
```go stmt := spanner.Statement{SQL: "SELECT Id, Name FROM Users"} iter := client.Single().Query(ctx, stmt) defer iter.Stop() for { row, err := iter.Next() if err == iterator.Done { break } if err != nil { log.Fatal(err) } var id int64 var name string if err := row.Columns(&id, &name); err != nil { log.Fatal(err) } fmt.Printf("User: %d - %s\n", id, name) } ```
-
Updating data in the database:
```go _, err := client.Apply(ctx, []*spanner.Mutation{ spanner.Update("Users", []string{"Id", "Name"}, []interface{}{1, "Jane Smith"}, ), }) if err != nil { log.Fatal(err) } ```
-
Deleting data from the database:
```go _, err := client.Apply(ctx, []*spanner.Mutation{ spanner.Delete("Users", spanner.Key{1}, ), }) if err != nil { log.Fatal(err) } ```
Conclusion
In this tutorial, we have learned how to build a Go application for managing a Google Cloud Spanner database. We covered the setup process, creating a GCP Spanner database, connecting to the database from Go, and performing CRUD operations. This knowledge will enable you to effectively utilize the GCP Spanner database for your applications.