Tag 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.

pprof quick start: profile a Go service in 10 minutes
10 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.

Flattening the Package Structure
3 min read

Creating an organized code structure can be a complex endeavor. Previously, I penned a blog post entitled How to structure Go code?, an attempt to demystify this topic. While I stand by the insights shared, I’ve come to realize that the article is somewhat generic and lacks clear, tangible answers to the question at hand.

There’s arguably no better way to understand such concepts than by diving into concrete examples. Today, I’d like to discuss the evolution of a package named productcatalog and explain why I chose to streamline its structure.

New project: ecommerce
3 min read

I’m excited to share with you my new project - an open-source e-commerce platform. The frontend is built with ReactJS and the backend is written in Go.

Project Goals

The goal of this project is to:

  • Continuously improve and develop the platform.
  • Provide an opportunity for less experienced programmers to gain experience working on a real project.
  • Experiment with various methodologies and tools that may not be available in other work settings (event-driven architecture, DDD, event sourcing, and more).
  • Build an application from scratch, including the entire ecosystem (frontend, backend, database, infrastructure, monitoring, observability).

Good Development Practices

Additionally, I strive to follow best practices in software development, such as writing Architecture Decision Records (ADRs) and thorough documentation. I believe that these practices help ensure the long-term success of the project and facilitate collaboration among contributors.

Password policies
2 min read

One of tasks I was working on recently is related to password policies. Of course, everything is configurable in code right now. In this note I want to tell you about some my decisions and how I got to them.

My first idea was creating an interface that any policy will have to satisfy.

type PasswordPolicy interface {
    Verify(pass string) error
}

That make sense, doesn’t it? When I was working on specific policy implementation I had a feeling that the type doesn’t have to be an interface. A regular function should be enough. I rewroted it into fuctions then.

Memory-wall problem: cache locality in Go
9 min read

The memory wall problem refers to a phenomenon that occurs in computer architecture when the processor’s speed outpaces the rate at which data can be transferred to and from the memory system. As a result, the processor must wait for the data to be fetched from memory, which slows down its performance and limits its speed.

The memory wall problem has become increasingly significant as processors have become faster and more powerful while memory speeds have not kept pace with these advancements. This means that even though the processor can execute instructions quickly, it spends a significant amount of time waiting for data to be transferred to and from memory.

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.