Checkpoints
Check Firestore Database Deployment
/ 25
Check Cloud Shell application Deployment
/ 25
Check App Engine Application Deployments
/ 25
Check App Engine application Deployment
/ 25
Use Go Code to Work with Google Cloud Data Sources
- GSP701
- Overview
- Setup and requirements
- What is Go?
- Deployment
- Task 1. Prepare your environment
- Task 2. Review the Google Cloud Data Drive source code
- Task 3. Check the import process
- Task 4. Compile and run Google Cloud Data Drive application in Cloud Shell
- Task 5. Test the application in your browser
- Task 6. Deploy to App Engine
- Congratulations!
GSP701
Overview
Go is an open source programming language supported by Google that makes it easy to build fast, reliable, and efficient software at scale. In this lab you explore the basics of Go by compiling and testing a Go app interactively in Cloud Shell, and then deploying it to App Engine. You then use the Go app to access data in BigQuery and Firestore.
What you'll do
In this lab, you perform the following:
- Code review of the open source tool developed by Google
- Explore code patterns that extract data from Google Cloud
- Test your Go app from a browser
- Deploy your Go app to App Engine
- Test your deployed Go app from a browser or using
curl
commands
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
What is Go?
Go (golang) is a general-purpose language designed with systems programming in mind. It is strongly typed and garbage-collected and has explicit support for concurrent programming. Programs are constructed from packages, whose properties allow efficient management of dependencies.
Go runs native on Google Cloud, and is fully supported on Google Kubernetes Engine, Compute Engine, App Engine, Cloud Run, and Cloud Functions. Google Cloud has open source client libraries for Google Cloud service which can be found on GitHub. Go is also used in production by thousands of enterprises, including some of Google Cloud's largest customers. Learn how enterprises develop with Go at Go.dev.
Unlike Python and Javascript, Go is a compiled language and not interpreted. Go source code is compiled into machine code before execution. As a result, Go is typically faster and more efficient than interpreted languages and does not require a runtime like Node, Python, or JDK to execute.
Build for any platform (OS/CPU) from any platform (OS/CPU)
The cross compile feature of Go streamlines the build process and makes Go one of the most portable languages in existence. You can build a Go program for any supported platform and CPU type, from any platform and CPU type. For example, if you are developing a Go program on OS X, you can simply change build parameters to build that application as an .exe
file that will run on Windows. Cross compilation in Go simplifies CI/CD pipelines and testing.
Function over construction
Go focuses on function over construction. This means you spend less time worrying about program construction and more time accomplishing project objectives.
Keep it simple and consistent
A few simple commands are all you need to deploy your source code across a range of Google Cloud Serverless technologies. This makes it easy to deploy Go apps on Google Cloud.
Go Compatibility
Source code written in Go is expected to have a long life. For the specifics of Go compatibility, see Go 1 and the Future of Go Programs. In short, Go code written using the Go 1 guidelines will continue to compile and run for the lifetime of Go 1 (10 years and climbing).
Deployment
Google Cloud offers several options for running your code. In this introduction to running a basic Go app, you deploy and test a Go app interactively, in Cloud Shell, and on App Engine.
App Engine
App Engine is well suited for running Go apps. App Engine is a serverless compute platform that is fully managed to scale up and down as workloads demand. Go apps are compiled to a single binary executable file during the deployment. Cold Start (zero instances running) request initial response times are often between 80 and 1400 milliseconds. Hot (at least 1 instance running ) requests are fulfilled immediately. Go apps running on App Engine give you the best of the cloud by being extremely efficient with CPU and memory resources at cloud scale with the App Engine service.
Architecture
The data flow architecture of a Go app deployed on App Engine looks like the diagram below:
Google Cloud Data Drive source code
In this lab, you’ll deploy a sample Go app called Google Cloud Data Drive. This app is developed by Google to help developers extract data from Google Cloud quickly, and is one of a series of tools that demonstrate effective usage patterns for using Cloud APIs and services.
Google Cloud Data Drive leverages a composable URL path to retrieve data in JSON format from two supported Google Cloud data platforms: BigQuery and Firestore.
Task 1. Prepare your environment
- In Cloud Shell, enter the following command to create an environment variable to store the Project ID for this lab:
Cloud Shell is developer ready and pre-configured with Go and many other languages.
- Enter the following command in Cloud Shell to view the version:
Example output:
- Get the sample code for this lab by cloning the sample repository:
Prepare your databases
This lab uses sample data in BigQuery and Firestore to test your Go app.
BigQuery database
BigQuery is a serverless, future proof data warehouse with numerous features for machine learning, data partitioning, and segmentation. BigQuery lets you analyze gigabytes to petabytes of data using ANSI SQL at blazing-fast speeds, and with zero operational overhead.
The BigQuery dataset is a view of California zip codes and was created for you when the lab started.
Firestore database
Firestore is a serverless document database with super fast document lookup and real-time eventing features. It is also capable of a 99.999% SLA. To use data in Firestore to test your app, you must initialize Firestore in native mode, then import the sample data into a storage bucket.
- When the database is ready, in Cloud Shell run the following to launch a Firestore import job that provides sample Firestore data for the lab:
It takes a few minutes to import data into Firestore. The import process loads a Firestore backup of a collection called symbols
into the $PROJECT_ID-firestore
storage bucket. This is sample data that is common to retailers and contains UPC and product information.
While you wait, continue to the next section.
Task 2. Review the Google Cloud Data Drive source code
In this lab you use the Google Cloud Data Drive application to demonstrate basic structure and effective usage patterns for using Go to access Cloud API's and services. Google Cloud Data Drive has been written not only for this lab but as something you could implement in your environment.
The Google Cloud Data Drive Go app leverages a simple composable URL path to retrieve data in JSON format from supported Google Cloud data platforms. The Google Cloud Data Drive app supports BigQuery and Firestore, but could be modified to support any data source. For more details see Google Cloud Data Drive on GitHub.
The main.go
file
The DIY-Tools/gcp-data-drive/cmd/webserver/main.go
file in the project is the entry point for the application. There is nothing special about the name main.go
. The compiler is looking for the package main
(below) and the function main
.
To get familiar with the structure of a Go program, view the code in this file in GitHub.
Package name: The package name "main" is used when the source code is executed directly. There are some exceptions to this, but for now consider anything with a package name "main" to be the first source code to get executed. For more information, see The Go Programming Language Specification for Packages.
The package definition defines the package that this source file belongs to, in this case main
:
Imports: Import statements define what internal or external Go packages to use in the source code. A short path, like "log" or "net/http", indicates that the package is included in the Go installation. By contrast, an import of github.com/foo
would cause Go to retrieve the external dependency from GitHub at build time. For a more detailed explanation of Go imports refer to The Go Programming Language Specification for Import declarations.
This import statement lists the packages used for this app:
Functions: Go function declarations name a block of Go source code and defines inputs and outputs for that source. The combination of the function name, input parameters, and outputs are also known as the function signature. For more information, refer to The Go Programming Language Specification on Function declarations.
The main
function
The main
function, shown below, is in the DIY-Tools/gcp-data-drive/cmd/webserver/main.go
file.
The func
keyword names a block of code. In this case "main
" is the name of the function definition.
- It adds an HTTP handler that specifies the function
gcpdatadrive.GetJSONData
. This function processes and responds to HTTP traffic. - It defines the access ports used to listen for HTTP requests, setting the default to 8080 (
port = 8080
). - Tells you what to do: listen and respond to HTTP traffic, and if there's an error, log the error.
With this very small block of Go code, you have created a very simple HTTP server.
Connect to Cloud services with Go
In this section you explore how to connect to Google Cloud data platforms. The code needed to retrieve data from each data platform differs based on the underlying technology your code needs to access. This varies because the individual Google Cloud data platforms differ in the problems they solve and how the data they store is accessed.
BigQuery is a serverless, future proof data warehouse with numerous features for machine learning, data partitioning, and segmentation. This lets you analyze gigabytes to petabytes of data using ANSI SQL at blazing-fast speeds, and with zero operational overhead.
Firestore is a serverless document database, with super fast document lookup and real-time eventing features. It is also capable of a 99.999% SLA.
Go excels at masking the complexity that arises when working with different platforms like these while surfacing a simple API. You will explore that later; for now, look at how data are extracted from BigQuery using Go.
Getting Data from BigQuery
The DIY-Tools/gcp-data-drive/bq.go
file contains the Go code that extracts data from BigQuery.
In this snippet notice the b.query.Read()
statement. In this context, b
represents a variable that was passed to this function as a pointer to a bqdataplatform
. The variable b
is a memory pointer to a struct of type bqdataplatform
that has a property named query
, and query
has a function named Read
.
The call to the Read
function returns results, so we collect those results and encode them to JSON.
In the following snippet, the for
statement collects the results into a slice of map named res
. Once the results are gathered they are encoded to JSON in the return
statement.
Getting data from Firestore
The DIY-Tools/gcp-data-drive/fs.go
file contains the Go code that retrieves data from Firestore. This code is similar to the code used to get data from BigQuery in the previous section.
Firestore has the concept of a collection/document/collection/document pattern to store documents. In the case of Firestore we can get either just a single document or a list of documents in a collection.
Go structs and interfaces
You've just seen how to pull data from a Cloud data platform such as BigQuery and Firestore. In this section look at the overall Go program structure and see how the demo application for this lab, Google Cloud Data Drive, is written in a modular way to support any number of underlying data platforms.
The Google Cloud Data Drive app is a JSON data service that retrieves data from either BigQuery or Firestore. You will deploy this service on App Engine later in this lab to see it in action. You use the following HTTP URL patterns to get data from Cloud platforms via gcp-data-drive
.
-
BigQuery:
https://<<host>>/bq/projectid/dataset/view
-
Firestore:
https://<<host>>/fs/projectid/collection/document...
The host in these examples is the URL you use to access the server or service that gcp-data-drive
runs on, or is deployed to.
- Return to
DIY-Tools/gcp-data-drive/webserver/main.go
to walk through an HTTP request to see how this reusablity is accomplished.
Notice in the func main()
the HTTP listener is specified by the http.HandleFunc
function. This function takes two parameters: a path string and a function. Yes, functions can be parameters in Go. The http.HandleFunc
function call defines the function that is used to process requests for the specified path. In this case, since the path is "/"
, all HTTP requests are sent to a function in the gcpdatadrive
module called GetJSONData
and the request data, for example the full URL request path, are passed as parameters to that function.
http.ListenAndServeTLS
and providing a PEM
file containing the certificate you want to use.
- Now take a look at the
DIY-Tools/gcp-data-drive/gcpdatadrive.go
file.
Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here. Interfaces with only one or two methods are common in Go code, and are usually given a name derived from the method, such as io.Writer for something that implements Write. For more information, see The Go Programming Language Specification on Interface types.
- See the line
type dataplatform interface
.
A Go interface is arguably the most valuable feature in Go. In the case of gcp-data-drive
a dataPlatform interface
is created to group methods together. When a struct type is created in Go and it has the same methods as an interface, the struct type is said to have "implicitly implemented" the Go interface. More details will come later, for now just remember that Go interfaces are groups of methods and Go types implement Go interfaces implicitly.
In this next code snippet, you see the definition of the GetJSONData
function. This function was referenced in main.go
as the code to be used as the HTTP handler function.
GetJSONData
takes an http.ResponseWriter
and a http.Request
as parameters. Each time an HTTP request is received in the main function, this GetJSONData
function is called.
Notice the line conParams, err := parseDDURL(r)
. This line calls another function, parseDDURL
, that parses the URL contained in the parameter r, which is an http.Request
.
Line pd, err := parseDataPlatform(r.Context(), conParams)
, calls the parseDataPlatform
function:
The parseDataPlatform
function identifies the data platform and returns the function that is used to query data from that platform or provide an error message detailing the platforms that are supported.
Go has a switch flow control statement as you might expect in any language. In this case you are switching on the platform indicator provided in the URL. Remember the request format that the gcp-data-drive
application expects is in the format https://<host>/<platform>/...
and the <platform>
position in the URL determines which function to call, newBQPlatform()
for BigQuery or newFSPlatform()
for Firestore. The source of these functions are in bq.go
and fs.go
.
Pay attention to the return statements of the parseDataPlatform()
function. Each of these methods return different Go types, but because each data platform type implicitly implements the methods of the dataplatform interface, you can return it to the GetJSONData
function.
Back in the GetJSONData
function, be sure that your getData
method is available because it is required for the dataPlatform
interface:
Go interfaces are a big topic, and this lab merely scratches the surface. The key takeaway is that even though Go is a statically typed and compiled language, using Go interfaces can make modular programs and services that maximize code reuse.
To see additional data platform support, feel free to create an issue or submit a pull request on GitHub.
Task 3. Check the import process
- Look in Cloud Shell to see if the import process loading the data into Filestore has finished.
Task 4. Compile and run Google Cloud Data Drive application in Cloud Shell
This section shows how easy it is to interactively compile and run Go apps.
- In Cloud Shell, change to the directory for the application that you cloned from GitHub:
- Compile the application:
This saves the compiled application into the gcp-data-drive
binary in the current directory. Since this is the first use of go build on this machine, you see Go download and provision several external libraries. Each library is cached so additional builds are fast. Run the same command again and notice the difference.
- Run the application locally to test it interactively:
Leave the application running in this Cloud Shell tab.
Task 5. Test the application in your browser
The Google Cloud Data Drive app responds to HTTP requests, parsing the request URL to extract parameters about the target Cloud data service. Once the application has those parameters it queries the required platform and returns data in JSON format.
These HTTP URL patterns are used to get data from Google Cloud using the Google Cloud Data Drive app.
-
Firestore :
[SERVICE_URL]/fs/[PROJECT_ID]/[COLLECTION]/[DOCUMENT]
-
BigQuery:
[SERVICE_URL]/bq/[PROJECT_ID]/[DATASET]/[TABLE]
Where:
Parameter |
Description |
[SERVICE URL] |
The base URL for the application from App Engine or Cloud In this case, local running of the application, it is the first part of the URL from the Web Preview, similar to this:
|
[PROJECT ID] |
The Project ID of the Firestore Collection or BigQuery Dataset you want to access. You find the Project ID in the left panel in your lab. |
[COLLECTION] |
The Firestore Collection ID ( |
[DOCUMENT] |
The Firestore Document you would like to query ( |
[DATASET] |
The BigQuery Dataset name ( |
[TABLE] |
The BigQuery Table name ( |
Query Firestore using Google Cloud Data Drive
- In the Cloud Shell toolbar, click the + icon next to the first Cloud Shell tab to open a second Cloud Shell tab.
- Enter the following command to create an environment variable to store the Project ID for this lab:
- Click the Web Preview button and select Preview on port 8080 in your Cloud Shell tab.
This will open a new browser tab with Cloud Shell's service url.
- Copy the service url (your web preview page) without the
/?authuser=0
at the end.
What you copy will look similar to this:
- Return to your Cloud Shell terminal and paste the URL into a variable:
- Run the following command in the second Cloud Shell tab to generate the test URL that will access the application running in the first Cloud Shell tab, which queries the Firestore
symbols/product/symbol
collection:
The Web Preview URL is output and looks similar to this:
Service_URL
and Project_ID
.
- Click the Web Preview URL.
This URL opens in a new browser tab, and is redirected to the Web Preview for Cloud Shell with the fully composed URL. This composed URL queries the symbols/products/symbol
Firestore collection in your project.
The following is a portion of the data you should see returned:
Query BigQuery using Google Cloud Data Drive
- In the browser tab that queried the Firestore collection, update the URL. Replace:
-
fs
withbq
-
symbols/product/symbol
withpublicviews/ca_zip_codes?authuser=0&environment_name=default
for the sample BigQuery table that is included in the lab for testing.
The URL should look something like this:
Service_URL
and Project_ID
.
You should see a page of results containing JSON data similar to the following:
- Close the preview browser tab.
- Close the second Cloud Shell tab.
- Back in the first Cloud Shell tab, and press Ctrl+C to stop the Google Cloud Data Drive application.
Go makes the complex simple. One of Go's strengths is masking complexity (in this case Google Cloud access) into simple to use building blocks.
Task 6. Deploy to App Engine
One of the most valuable features of Go is its portability. In this section you deploy the Google Cloud Data Drive app to App Engine without making any changes to the code. Deploying to App Engine allows you to maintain a running copy of your application without having to manage server infrastructure.
Examine the source code
To deploy a Go app to App Engine standard environment, you must provide an app.yaml
file containing the application's service configuration settings. For the Google Cloud Data Drive application, the DIY-Tools/gcp-data-drive/cmd/webserver/app.yaml
file contains the following:
This specifies the Go runtime to be used, which URL requests to handle, and which port to redirect inbound web requests to. The Google Cloud Data Drive app sets its HTTP listener port to the value of the PORT environment variable. App Engine requires applications to listen on port 8080.
Deploy the Google Cloud Data Drive app
- In Cloud Shell, make sure you're still in the directory for the app you cloned from GitHub:
- Use sed command to upgrade your
Go runtime
verison in app.yaml file:
- Deploy the Google Cloud Data Drive app to App Engine:
The deployment takes up to 2 minutes.
target url
in the output is the endpoint of the application, which you will use to test the application.
- Store the App Engine URL in an environment variable to use when you test the application:
This command extracts the target url
for the first application in your project and stores it in an environment variable.
Test the application
You deployed the application to App Engine, now try it out.
Access the application in your browser
-
To access the application in your browser, construct the URL in the same way as you did when you access the application locally:
-
Firestore:
[SERVICE_URL]/fs/[PROJECT_ID]/[COLLECTION]/[DOCUMENT]
-
BigQuery:
[SERVICE_URL]/bq/[PROJECT_ID]/[DATASET]/[TABLE]
[SERVICE_URL]
Where:
Parameter |
Description |
[SERVICE URL] |
The base URL for the application from App Engine or Cloud In this case, running the application on App Engine, it is the
|
[PROJECT ID] |
The Project ID of the Firestore Collection or BigQuery Dataset you want to access. You find the Project ID in the left panel in your lab. |
[COLLECTION] |
The Firestore Collection ID ( |
[DOCUMENT] |
The Firestore Document you would like to query ( |
[DATASET] |
The BigQuery Dataset name ( |
[TABLE] |
The BigQuery Table name ( |
Access the application with the curl
command
- Use
curl
to call the application running on App Engine to query data from Firestore:
This returns the same data that you saw previously in the preview browser window:
- Drill down to see the UPC data for a product in the Firestore product collection:
You should see the following data returned:
- Use curl to call the application running on App Engine to query data from BigQuery:
This returns the same data that you saw previously in the preview browser window:
Congratulations!
You successfully deployed and tested a Go app that is capable of retrieving and returning data as a rest API at Google scale using the Cloud Shell and App Engine standard environment. You have seen how easy it is to access Google data platforms via Go and how Go’s code portability allows you to deploy it to a serverless platform, like App Engine, without modifications.
Finish your quest
This self-paced lab is part of the Getting Started with Go on Google Cloud quest. A quest is a series of related labs that form a learning path. Completing this quest earns you a badge to recognize your achievement. You can make your badge or badges public and link to them in your online resume or social media account. Enroll in this quest or any quest that contains this lab and get immediate completion credit. See the Google Cloud Skills Boost catalog to see all available quests.
Take your next lab
Deploy Go Apps on Google Cloud Serverless Platforms.
Next steps/Learn more
Go is a great programming language for Google Cloud. To learn more about programming in Go have a look at these resources:
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual last updated April 08, 2024
Lab last tested April 08, 2024
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.