Go hello world
This example is a very simple "hello world" application, written in Go, that illustrates how to:
- Set up authentication
- Connect to a Bigtable instance.
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Set up authentication
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Running the sample
This example uses the Cloud Bigtable package of the Google Cloud Client Library for Go to communicate with Bigtable.
To run this sample program, follow the instructions for the sample on GitHub.
Using the Cloud Client Library with Bigtable
The sample application connects to Bigtable and demonstrates some simple operations.
Importing the client library
The sample uses the following imports:
Connecting to Bigtable to manage tables
To manage tables, connect to Bigtable using
bigtable.NewAdminClient()
.
Creating a table
Create a table with AdminClient.CreateTable()
, then get
information about the table with AdminClient.TableInfo()
.
Create a column family with
AdminClient.CreateColumnFamily()
.
Connecting to Bigtable to manage data
To manage data, connect to Bigtable using
bigtable.NewClient()
.
Writing rows to a table
Open the table you want to write to. Use
bigtable.NewMutation()
to create a mutation on a single
row, then use Mutation.Set()
to set values in the row.
Generate a unique row key for each row. Repeat these steps to create multiple
mutations. Finally, use Table.ApplyBulk()
to apply
all of the mutations to your table.
Reading a row by its key
Get a row directly using its key with Table.ReadRow()
.
Scanning all table rows
Use Table.ReadRows()
to scan all of the rows in a table.
Close the data client when you are done using it.
Deleting a table
Delete a table with AdminClient.DeleteTable()
. Close the
admin client when you are done using it.
Putting it all together
Here is the full example without comments.