1. Home
  2. Let's Build a Microservice with Golang: A Step-by-Step Guide

Let's Build a Microservice with Golang: A Step-by-Step Guide

If you're looking to build a resilient, scalable, and easily deployable system, microservices are an excellent choice. And if you want to take things to the next level with your microservices, it's worth considering a language like Go (Golang).

In this step-by-step guide, we'll walk you through everything you need to know to build a microservice with Golang. We'll cover setting up the environment, writing the code, testing the microservice, and deploying it to a cloud provider.

Why Golang?

Golang is an open-source programming language created by Google in 2009. It's designed for building efficient, readable, and scalable software. Golang is particularly well-suited for building microservices, thanks to its lightweight threads called "goroutines," its efficient memory management, and its built-in concurrency support.

Setting Up the Environment

Before we can start building our microservice, we need to set up our development environment. Here's what you'll need:

  • Go installed on your machine (version 1.16 or later)
  • A text editor or integrated development environment (IDE)
  • Docker installed on your machine (if you plan to deploy the microservice in a container)

Once you have those tools installed, you can create a new project directory and initialize a new module:

mkdir my-service && cd my-service
go mod init example.com/my-service

Writing the Code

Now that our environment is set up, we can start writing the code for our microservice. For this example, we'll build a simple HTTP server that returns information about the current time.

Create a new file called main.go, and add the following code:

package main

import (
	"fmt"
	"net/http"
	"time"
)

func main() {
	http.HandleFunc("/", getTime)
	http.ListenAndServe(":8080", nil)
}

func getTime(w http.ResponseWriter, r *http.Request) {
	currentTime := time.Now().Format("2006-01-02T15:04:05.999Z")
	fmt.Fprintf(w, "The current time is: %s", currentTime)
}

This code defines an HTTP route at the root path ("/"), which calls the getTime function when a request is received. The getTime function formats the current time and sends it back to the client.

Testing the Microservice

Before we deploy our microservice, we need to test it out locally to make sure it's working correctly. To do this, run the following command:

go run main.go

This will start the server on port 8080. You can test the microservice by opening a web browser and navigating to http://localhost:8080.

Deploying to a Cloud Provider

Now that we've tested our microservice locally, we can deploy it to a cloud provider. There are many cloud providers to choose from, but for this example, we'll use Google Cloud Platform.

Step 1: Create a Google Cloud Account

If you don't already have a Google Cloud account, sign up for one at https://cloud.google.com/free. You'll need to provide a credit card, but Google offers a free tier with $300 of credit to get you started.

Step 2: Set Up a Google Cloud Project

Once you've signed up for a Google Cloud account, create a new project by navigating to the Google Cloud Console at https://console.cloud.google.com. In the top navigation bar, you should see a dropdown labeled "Select a project." Click on this dropdown, and then click the "New Project" button.

Give your project a name and a unique ID, and then click the "Create" button.

Step 3: Deploy the Microservice to Google Cloud Run

With our Google Cloud project set up, we're ready to deploy our microservice. To make deploying the microservice as easy as possible, we'll use Google Cloud Run, which is a serverless platform for running containerized applications.

To deploy our microservice to Google Cloud Run, follow these steps:

  1. Open a terminal window and navigate to your project directory.

  2. Build a container image of your microservice using the following command:

    docker build -t gcr.io/[PROJECT-ID]/my-service .

    Replace [PROJECT-ID] with the ID of your Google Cloud project.

  3. Push the container image to Google Cloud Container Registry using the following command:

    docker push gcr.io/[PROJECT-ID]/my-service
  4. Navigate to the Google Cloud Console, and select Google Cloud Run from the navigation menu on the left.

  5. Click the "Create Service" button.

  6. Select "Cloud Run (fully managed)" as the deployment platform.

  7. Give your service a name, and then click the "Next" button.

  8. Choose your container image, and then click the "Next" button.

  9. Configure your service by setting the maximum number of instances, the amount of memory, and the target CPU utilization. You can leave these at their default values for now.

  10. Click the "Create" button.

Google Cloud Run will now deploy your microservice to a container, and make it available at a URL like [SERVICE-URL].run.app. You can test your microservice by visiting this URL in a web browser.

Conclusion

In this post, we've learned how to build a microservice with Golang, test it locally, and deploy it to a cloud provider. With these skills under your belt, you can start building more complex microservices that are scalable, efficient, and easy to deploy.

If you want to learn more about Golang or microservices, check out some of the other posts on Devspedia. Happy coding!

This article was written by Gen-AI GPT-3. Articles published after 2023 are written by GPT-4, GPT-4o or GPT-o1

1393 words authored by Gen-AI! So please do not take it seriously, it's just for fun!