Query a public dataset with the BigQuery Go client library
- Activate Cloud Shell in a Google Cloud project.
- Open the Cloud Shell Editor.
- Prepare files for queries.
- Query a public dataset in BigQuery.
- Clean up.
Estimated time to complete:
Click Startto begin.
Activate Cloud Shell in a Google Cloud project
-
If you do not enable billing for a project, you automatically work in the BigQuery sandbox. The BigQuery sandbox lets you learn BigQuery with a limited set of BigQuery features at no charge. If you do not plan to use your project beyond this document, we recommend that you use the BigQuery sandbox.
- Click Activate
Cloud Shell.
Show me
To learn how to open the Cloud Shell Editor, click Next.
Open the Cloud Shell Editor
-
In Cloud Shell, create a new Go project and file:
mkdir bigquery-go-quickstart \ && touch \ bigquery-go-quickstart/app.goThis command creates a Go project that's named
bigquery-go-quickstartand a file that's namedapp.go. -
Open the Cloud Shell Editor:
cloudshell workspace bigquery-go-quickstart
To learn how to prepare files for queries, click Next.
Prepare files for queries
-
To open a terminal in the Cloud Shell Editor, click Open Terminal.
-
Open your project directory:
cd bigquery-go-quickstart -
Create a
go.modfile:go mod init quickstartThe output is similar to the following:
go: creating new go.mod: module quickstart go: to add module requirements and sums: go mod tidy -
Install the BigQuery client library for Go:
go get cloud.google.com/go/bigqueryThe output is similar to the following. Several lines are omitted to simplify the output.
go: downloading cloud.google.com/go/bigquery v1.49.0 go: downloading cloud.google.com/go v0.110.0 ... go: added cloud.google.com/go/bigquery v1.49.0 go: added cloud.google.com/go v0.110.0
To learn how to query a public dataset in BigQuery, click Next.
Query a public dataset in BigQuery
-
Click Open Editor.
-
In the Explorerpane, locate your
BIGQUERY-GO-QUICKSTARTproject. -
Click the
app.gofile to open it. -
To create a query against the
bigquery-public-data.stackoverflowdataset that returns the top 10 most viewed Stack Overflow pages and their view counts, copy the following code into theapp.gofile:
Click Open Terminal.
In the terminal, run the app.go
script. If you are prompted to authorize
Cloud Shell and agree to the terms, click Authorize.
go
run
app.go
The result is similar to the following:
https://stackoverflow.com/questions/35159967 : 170023 views
https://stackoverflow.com/questions/22879669 : 142581 views
https://stackoverflow.com/questions/10604135 : 132406 views
https://stackoverflow.com/questions/44564887 : 128781 views
https://stackoverflow.com/questions/27060396 : 127008 views
https://stackoverflow.com/questions/12482637 : 120766 views
https://stackoverflow.com/questions/20673986 : 115720 views
https://stackoverflow.com/questions/39109817 : 108368 views
https://stackoverflow.com/questions/11057219 : 105175 views
https://stackoverflow.com/questions/43195143 : 101878 views
You have successfully queried a public dataset with the BigQuery Go client library.
To avoid incurring charges to your account and learn about next steps, click Next.

