Category Golang

featured.png

Go is perfect for network applications. Its awesome standard library helps a lot in writing such software. In this article, we’ll write a simple TCP scanner in Go. The whole programm will take less than 50 lines of code. Before we’ll go to practice - a little theory. Of course, the TCP is more complicated than I describe but we need just basics. The TCP handshake is three-way. Firstly, the client sends the syn package which signals the beginning of a communication.

featured.png

In Go 1.13 all modules are provided using a proxy. The proxy caches dependencies what helps to make sure that the version of an external dependencies will never change. If the vendor remove the version and create a new one with the same version, only the first one will be provided. Proxy improves the performance of downloading dependencies as well so it’s useful to have such functionality in the ecosystem.

featured.png

Structuring the source code can be as challenging as writing it. There are many approaches to do so. Bad decisions can be painful and refactoring can be very time-consuming. On the other hand, it’s almost impossible to perfectly design your application at the beginning. What’s more, some solutions may work at some application’s size and should the application develop over time. Our software should grow with the problem it’s solving.

featured.png

Testing is one of the hardest stuff in programming. Today trick will help you organize your tests and the production code. Let’s assume you have a package called orders. When you want to separate the package for tests from the production code you can create a new folder and write tests there. It will work but there’s a more clearer way - put your tests to the folder with you package but suffix the package’s name in tests with _test.

featured.png

While working with structures, there’s a possibility to initialize the structure without providing the keys of fields. type SomeSturct struct { FirstField string SecondField bool } // ... myStruct := SomeSturct{"", false} If we want to force other (or even ourselfs) to explicitly providing the keys, we can add _ struct{} in the end of the structure. type SomeSturct struct { FirstField string SecondField bool _ struct{} } // COMPILATION ERROR myStruct := SomeSturct{"", false} The code above will produce too few values in SomeSturct literal error.

featured.png

While developing a library, we create a directory structure to keep the code organized. However, some exported functions or struct should not be used by users of the library. The achieve that, call the package internal. . ├── bar │ ├── bar.go │ └── internal │ └── foobar.go ├── internal │ └── foo.go └── main.go In the example above, the foo.go can be included only in the main.go. What’s more, only the bar.

featured.png

In the microservices’ world, one thing what’s worth considering is a graceful shutdown. This is important to not lose data while shutting down a container. The container orchestrator like Kubernetes can restart the container by sending SIGTERM or SIGINT signal. Those signals can be handled to safely close all connections and finish background tasks. Signals are propagated using os.Signal channel. You can add the above code to your main. var gracefulStop = make(chan os.

featured.png

When it comes to interfaces, a good practice is to create an interface where you’ll use it. Creating interfaces in advanced is not recommended in Go. There are two exceptions: you’re creating a library which will be used in different projects you’ll have more than 1 implementation In the example below, we have a storage implementation. type inMemoryStorage struct { mutex *sync.Mutex storage map[string]*Value } func NewStorage() *inMemoryStorage { return &inMemoryStorage{ storage: map[string]*Value{}, mutex: &sync.

featured.png

You should use the package github.com/pkg/errors instead of errors package for errors in your applications. The default package lacks a few things like: stack trace easy appending message to the error and more It helps with debugging a lot. Below you can find an example error message with the stack trace. An important thing to remember is that you should wrap every error which is from any external library or your freshly created error you want to return.

featured.png

Golang uses a connection pool to manage opened connections for us. As a result, new connections are used when no free connection left and reuses them when golang finds an idle connection. The most important thing is that when two queries are called one by one it does not mean that the queries will use the same connection. It may be true if not in every case. In the example below, you can find two queries which may seem to be executed in one connection.