Category Golang

Part 2: CPU Profiling Deep Dive - Understanding Where Time Actually Goes
16 min read

My service was slow at 28ms per request. I could have guessed the cause - database, JSON, strings? Instead, I profiled it. The results surprised me.

Part 1: pprof Quick Start - Get Profiling in 10 Minutes
8 min read

I spent two weeks optimizing a function that accounted for 0.3% of my program’s runtime. Meanwhile, a JSON call was eating 45% of CPU. pprof would have shown me in seconds.

Go libs I don't use but are popular
4 min read

Introduction

In the Go programming ecosystem, developers have a plethora of libraries available for solving common problems. However, some libraries may not always be the best fit for every project or developer preference. This article highlights a few Go libraries that I personally avoid using, along with the reasons behind these choices. The intention is not to discourage the use of these libraries universally but to shed light on potential challenges that may arise when using them, especially in larger or more complex projects.

Testing code with unpredictable or random output
4 min read

Introduction

In this blog post, I want to share my approach to testing functions involving randomness in Go. Recently, I was asked how I would test a specific function that calculates possible directions for an object to move. Initially, I didn’t come up with a good idea. Here, I’ll discuss how I’d solve this problem in a real-world application with a detailed explanation.

The Function in Question

The function calculates all possible directions that an object can move (up, down, left, right) without violating boundaries. It then randomly selects a valid direction and returns the new coordinates.

Why We Should Avoid Using `else` in Programming
3 min read

The else keyword is a commonly used control structure in programming. It allows us to execute a block of code if a condition is not true. However, overusing else statements can lead to less readable and maintainable code. In this article, we’ll explore why we should avoid using else clauses in our code and look at some alternatives that can make our code more concise and readable.

Why Overusing else is a Bad Idea

One of the main arguments against using else statements is that they can make our code more complex and harder to read. As the number of conditions and branches increases, the code becomes harder to understand and maintain. Additionally, else clauses can make it difficult to follow the control flow of a program.

Writing tests in Go (business apps)
9 min read

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.

General rules for tests

Works out of the box

When someone clones our project, the person should be able to run basic tests without any setup. It’s a good thing when you have an open-source project as well as when you have a new team member. Or even for you after you reinstall your PC. I remember many projects where I had to spend a day or two to make it work and actually start developing something. It’s frustrating when you have to manually set up a DB connection, get proper permissions to an AWS account, configure it correctly, and so on.

Honestly about why Go sucks (or not)
12 min read

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.

Arguments agains the language

Lack of Function Overloading and Default Values for Arguments (https://www.toptal.com/go/4-go-language-criticisms)

Yes, Go doesn’t have those features. And probably will never have. The argument here is that developers have to write more code than they have to. Right now, we have to write functions like this:

Top level logging
5 min read

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. In most cases, the logger is a redundant dependency. In this article, I’ll argue that we should have the logger only in top-level functions.

`replace` directive in go modules
1 min read

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.com/my/library ../path

The path can be both relative (to the application root folder) or absolute. The go.mod file will be edited as follows.

gRPC with SSL/TLS
2 min read

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.

creds, err := credentials.NewClientTLSFromFile(certFile, "")
if err != nil {
    // handle the error - no ignore it!
}
conn, _ := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))

But, from where take the certificate? One of ways is using the openssl.