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 with SSL/TLS

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.

Wrapping commands in Go

You can find a lot of articles about Go that describe general aspects of it. Including the content on this blog. Today, I decided to prepare something different. I’ll tell you about one of my tasks and I’ll show you how I resolved it using Go. I thought it’d be useful to show the exec package and to tell a bit about the ssh command and learn AWS EE2 a bit better.

Periodically restarting apps on Kubernetes

Failures and downtime are part of our day-to-day life. I had a problem with one of the services that started crashing a few times a week. We noticed that it crashes because the memory usage reaches its limits no matter how high the limit is. Debugging memory leaks is hard and time-consuming. As a temporary fix[^Nothing is more permanent than a temporary solution] we decided to restart the application once a day.