Implementing a Go Service for Google Cloud Run

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating a Go Service
  5. Building and Deploying the Service
  6. Conclusion

Introduction

In this tutorial, we will learn how to implement a Go service for Google Cloud Run. Google Cloud Run is a fully managed serverless platform that allows you to run containers on Google Cloud. By the end of this tutorial, you will be able to create a simple Go service and deploy it on Google Cloud Run.

Prerequisites

To follow along with this tutorial, you will need:

  • Basic knowledge of Go programming language
  • A Google Cloud account

Setup

Before we start creating the Go service, we need to set up our development environment and ensure that we have the necessary tools and libraries installed.

  1. Install Go: If you don’t already have Go installed, you can download it from the official website (https://golang.org/dl/) and follow the installation instructions.

  2. Install Google Cloud SDK: The Google Cloud SDK provides the command-line tools necessary to interact with Google Cloud services. You can download and install the SDK from the official website (https://cloud.google.com/sdk/docs/install) according to your operating system.

  3. Authenticate with Google Cloud: After installing the SDK, open the terminal and run the following command to authenticate with your Google Cloud account:

    ```
    gcloud auth login
    ```
    
  4. Set the Google Cloud project: If you haven’t already created a project in Google Cloud, create one now. Then, set the project as the default for the gcloud command-line tool by running the following command:

    ```
    gcloud config set project PROJECT_ID
    ```
    
    Replace `PROJECT_ID` with your actual project ID.
    

Creating a Go Service

Now that our setup is complete, let’s start creating our Go service. In this example, we will create a simple HTTP server that listens on port 8080 and responds with a “Hello, World!” message.

  1. Create a new directory for your project:

    ```
    mkdir go-service
    cd go-service
    ```
    
  2. Create a new file named main.go and open it in a text editor:

    ```
    touch main.go
    ```
    
  3. Open main.go file and add the following code:

    ```go
    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    }
    
    func main() {
        http.HandleFunc("/", handler)
        http.ListenAndServe(":8080", nil)
    }
    ```
    
    This code defines an HTTP handler function `handler` that writes "Hello, World!" to the response writer, and a `main` function that registers the handler and starts an HTTP server listening on port 8080.
    
  4. Save the main.go file and exit the text editor.

Building and Deploying the Service

With our Go service implemented, let’s build and deploy it on Google Cloud Run.

  1. Build the Docker image: Google Cloud Run requires the service to be deployed in a Docker container. We need to build a Docker image of our Go service by running the following command:

    ```
    docker build -t go-service .
    ```
    
    This command instructs Docker to build an image with the tag `go-service`, using the current directory as the build context.
    
  2. Verify the Docker image:

    ```
    docker images
    ```
    
    You should see the `go-service` image listed among your Docker images.
    
  3. Deploy the service to Google Cloud Run:

    ```
    gcloud run deploy --image go-service --platform managed --allow-unauthenticated
    ```
    
    This command deploys the `go-service` image to Google Cloud Run as a managed service. The `--allow-unauthenticated` flag allows access to the service without authentication.
    
  4. Wait for the deployment to complete. After the deployment, you will see a URL where your service is accessible.

  5. Test the service: Open a web browser and navigate to the URL provided by the deployment. You should see the “Hello, World!” message displayed.

    Congratulations! You have successfully implemented a Go service for Google Cloud Run. You can now extend this service to perform more complex tasks or integrate with other Google Cloud services.

Conclusion

In this tutorial, we learned how to implement a simple Go service and deploy it on Google Cloud Run. We started by setting up our development environment and ensuring that we have the necessary tools installed. Then, we created a basic Go service that responds with a “Hello, World!” message. Finally, we built a Docker image of our service and deployed it on Google Cloud Run.