Writing tests in Go (business apps)

There are many practices and tactics that tackle testing. Today, I’ll share with you how I write tests for my projects. Please notice that you may find it useful when starting a new project or an independent part of existing applications. You may find it difficult to apply in an already existing application. It’s not impossible but it may be challenging. Table of content General rules for tests Works out of the box Single responsible As simple as possible Irrelevant code should be extracted How does the architecture of the package look like?

My Smart Home high-level architecture

I’m building a house. I want to make it “smart”. The goal is to save as much my (or my family’s) time as possible in the future. I needed a plan about how to do it. After some research I came up with the following architecture. Firstly, I needed a base. The base system that it will be the heart of everything. I had a few core requrements for it.

JSON in Go

In this article, I’ll tell you everything that you need to start using JSON in Go Fluent. We’ll start with some basic usage, we’ll talk about different ways of working in JSON and how to customize it. In the end, you’ll find a FAQ where I put the most common questions about JSON in Go. Table of content Basic usage Marshaling Unmarshalling Struct tags Encoder/decoder The performance? Indenting MarshalJSON and UnmarshalJSON UnmarshalJSON example FAQ What if I don’t know the schema?

Honestly about why Go sucks (or not)

Go is very opinionated. There are arguments that are based on personal preferences like “I don’t like the syntax” and much more specific. In this article, I’ll focus on the second type of arguments why Go isn’t the best language and confirm/denied them. My goal is to tell you the truth about the language. Table of content Arguments agains the language Lack of Function Overloading and Default Values for Arguments (https://www.

Top level logging

I like having the core logic of our application free of distractions like too many technical “details” like logging or generating metrics. Of course, sometimes it’s hard to avoid it. I found in many projects a situation where we put the logger very deeply inside of the code. At the end of the day, we had the logger almost everywhere. In tests, we had to provide the mocked implementation everywhere as well.

`replace` directive in go modules

Sometimes, we may want to use a library but a slightly modified version. It happens very often when we develop the library but test it in the context of an application. Go has a handy mechanism in go modules that can help us with it. To make it work, we have to clone the library somewhere near the target project and run the following command in the application’s folder. go mod edit -replace github.


gRPC supports authentication. Adding it to your project is simple. All you have to do is configure it with just a few lines of code. One of the authentication types that gRPC supports is SSL/TLS. From the server-side, the code looks like this: creds, err := credentials.NewServerTLSFromFile(certFile, keyFile) if err != nil { // handle the error - no ignore it! } s := grpc.NewServer(grpc.Creds(creds)) The client has to update the code as shown below.

How to structure Go code?

Programs should be written for people to read, and only incidentally for machines to execute - Abelson and Sussman It is one of the most popular questions. You can find on the Internet attempts to answer this question. I’ve had concerns if I’m designing my packages or even the whole project correctly. Today, I’m not 100% sure about that! Some time ago, I had the pleasure to meet Robert Griesemer (one of Go’s authors) in person.

HTTP context livetime

Some time ago, I found a Stack Overflow question. The author had a problem with understanding why the context from the request he’s using is canceled. I remember that I had a similar situation in the past: I used the context from the HTTP request and tried to use it in background operation and return the response to the user before it was finished. This issue comes from not understanding how the context is used in the http.