Golang

A 16-post collection

How To Create Health Check For RESTful Microservice In Golang

Health Check

Imagine you've recently released and deployed to production a cool RESTful microservice you worked on for a while. You heaved a sigh of relief just to hear from Ops team that your service is unstable. You are damn sure that the service should be fine, but you get a feeling that there could be something wrong with services it depends on. What should you do?

Health check will come to your rescue. It is an endpoint in your service returning status of your application including statuses of connections to all external services your service directly depends on. In this post

Read more »

Unit Testing Golang Code Calling Elasticsearch

There is an amazing Elasticsearch client for Go and in Working With Elasticsearch I demonstrated with example how to index and search for documents using it. However, if you want to ensure your code will continue working correctly and will not be inadvertently broken during refactoring, you have to cover the code with tests.

In this post I'll show how to unit test Go code interoperating with Elasticsearch. Keep in mind, however, that the same approach is applicable to unit testing almost any code calling external RESTful API.

A service calling Elasticsearch

Suppose you have a logging service which can

Read more »

How To Correctly Serialize JSON String In Golang

json is one of the most wildly used Go packages. It is simple and, what is more important, very intuitive. So what could be easier than marshalling a string with JSON and unmarshalling it to struct? If you believe (as I did) that the issue is trivial and json.Marshal does the job, read on.

What's wrong with json.Marshal?

It's easier to demonstrate on example. Let's write a simple program which serializes JSON string to bytes and deserializes the bytes into matching struct:

package main

import (  
    "encoding/json"
    "fmt"
)

type Person struct {  
    FirstName string `json:"firstName"`
    LastName  string `json:
Read more »

Working With Elasticsearch in Go

Elasticsearch is one of the most popular highly scalable full-text search engines on the market. It is based on Lucene engine and allows you to store, search, and analyze big volumes of data quickly and in near real time. It has rich REST API and clients for most popular programming languages including Go. In this post I'll demonstrate on example how to index and search for application logs using Go.

Installing Elasticsearch

If you haven't set it up already, installation of Elasticsearch is as easy as downloading it (from here) and running the executable file. I installed it on localhost

Read more »

Example Of Using Templates In Golang

Almost every programming language has a library implementing templating. In epoch of server side MVC dominance, templating was so important that it could determine language success or failure. Nowadays, however, when single page applications get momentum, templates are used only occasionally.

The niche where templates are still absolutely indispensable is email content generation. In this post I will demonstrate on an example how to use Golang template package to generate email content based on a template defined in a file. I'll keep example as simple as possible, but beefy enough to show most commonly used features of the package.

You

Read more »