Go is a popular language in the cloud environment. Many well-known applications are now indispensable in the cloud, like for example Docker, Kubernetes, Istio or Terraform were written in Go. This is also attested to by Steve Francia, product owner of Go at Google, in an interview from 2019 in which he addresses a conscious alignment of Go in the cloud environment and that too newly developed Go Cloud Development Kit. So it seems appropriate to take a closer look at the Lingua franca.
First things first
Go (also called Golang) is a compilable language that supports concurrency and has automatic garbage collection. In addition, the language syntax is minimalistic and based on the hardware-related C. The Go Programming Language Specification is just 50 pages long. The idea behind Go is to provide the smallest possible number of simple orthogonals instructions that can be assembled into a manageable number of patterns. This reduces the overall complexity. This makes code easier to write, understand, and maintain, as there is often only one way to go.
The features of Go result in a language that is particularly well suited for scalable, cluster-capable applications that are designed to solve exactly one task particularly well and efficiently. The Go compiler generates small binary artifacts that guarantee small footprints of Docker images. In addition, the Go Cloud Development Kit provides a library that covers common, cloud-typical operations such as reading blob storage (AWS S3) or health checks. For these reasons, Go is suitable for microservices.
Snippet under magnifying glass
To get a feel for the language right away, let’s examine two short code examples that illustrate Golang’s features. The first example is a simple object-oriented class that demonstrates the declaration of a data type and functions. The second example shows how to use concurrency in Golang with Goroutines and Channels. The snippets contain inline comments that explain the process.
A simple object-oriented class
The following snippet implements a simple, abstract data type Stack
in the package collection
using golang:
|
|
Implementation of goroutines and channels
Concurrency in Go relies on goroutines and channels. A goroutine is a parallel thread. Channels are connections that let goroutines communicate with each other. This allows certain parts of a program to run side by side and synchronously from other points.
The following snippet constantly writes out "ping"
.
The pinger()
function writes "ping"
to the channel variable c
.
The printer()
function outputs the content of c
every second via an intermediate variable msg
.
You can stop the constant output with Enter
.
|
|
The <-
operator sends values into the channel variable c
.
The value from the channel variable is read out again using the := <-
operator.
This value is routine-aware and is therefore always synchronous with the current value.
Microservice with Go
The basics of Go are covered, now let’s get our hands a little dirty.
A simple microservice typically exposes a set of data via an API using a REST interface
.
In our case, an array of cactus objects []Cactus
is provided through the interface.
Requirements
In order to carry out the following hands-on, the following tools are required:
- Go compiler installed
- Notepad (IntelliJ, VSCode, Atom, vim, …)
- Postman to test the API
- 5 minutes of your time
Resolve first the external library gorilla/mux
with the following command:
|
|
After that, create a file named cactusService.go
in any directory.
The go file must have the following content:
|
|
Now the go file can be compiled as follows. To do this, run the command in the same directory:
|
|
The resulting binary is 6.7 MB in size.
Now the REST interface can be called under localhost:5000/cactus/
.
Postman can be used to conveniently set the query parameters.
Congratulations, your first microservice in Go is functional! Now it is important to deepen this knowledge and to achieve a corresponding use case in your company. With Go, maintainable, concise services can be written that trump with small footprints and the Best Breeds of the cloud world.
Related Links:
Web-Frameworks
Buffalo | A Go web development eco-system, designed to make your life easier
Revel Framework | A high productivity, full-stack web framework for the Go language
Learning material about Go
Educative | Getting started with Golang: a tutorial for beginners
Golang | How to Write Go Code
This text was automatically translated with our golang markdown translator.