Practical Go articles by Bartłomiej Klimczak. No fluff, just code that works.
In this article, I explain how you can detect if the interface you’re using is getting too big and requires splitting into smaller ones. Smaller interfaces help to improve the maintenance and readability of the code. What’s more, it helps with understanding the code.
Interfaces in Go are different than those known in Java, c#, PHP etc. In those languages you define interfaces up-front. In other words, at the moment of creating a class you need to know how the class will be used. In Go things are different. You can create a struct with as many functions you want and the user of if can define only a sublist of methods he needs. It’s very powerful tool. But sometimes we still can create too big interfaces. The list above should help you find interface segragation issues in your code.
There are books that after reading you consider them as “must-read” and you’re asking yourself why you didn’t read it before. Today is the day when I read the “Deep Work: Rules for Focused Success in a Distracted World” by Cal Newport for the second time. I don’t regret it.
In COVID times, we very often work remotely. It changed a lot in our life as well as in the world. Working from home is cool but, on the other hand, very distracting. Being focused is important. I think we know that but do you know how much?
The context package in Go is quite simple and well-known. On the other hand, there are some misunderstandings while using it. Today, I’ll try to explain all the most popular concerns and make more clear when and how use the Context.
Let’s start with what the context is.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. ref: https://golang.org/pkg/context/
I always had problems with negotiations. To be precise - it’s still a problem for me but I think that “Never split a difference” by Chris Voss will help me with this. Here are some things I took down I remember from the book the most.
The first thing is the mood. When we feel safe and in control, we are more problem-solvers. A smile makes as more open and ready for negotiation instead of fight and resist. That sounds reasonable but the next thing surpassed me.
From time to time, I receive an email from a scammer that says he has X million dollars/euro for me. At the very beginning, I removed those emails but at some point, I decided to answer them. Here’s what I found. Every scammer starts very typically. There’s a very reach person who’s dying or very sick. They found my email on the Internet and learned that I’ll be the person who will spend the money wisely.
Go has plenty of different web frameworks. When you are faced with choosing a framework for the first time, it may turn out to be quite a challenge to choose the best one. This article is intended to help you choose the best one. It is full of personal judgments that you may disagree with. However, I believe you will find it most helpful.
Martini
The first framework is Martini. Honestly, it shouldn’t be here as it’s been under development since 2017. However, I added it because I found it on other compilations. Just don’t use it :)
Making changes to all HTTP requests can be handy. You may want to add an API key or some information about the sender like app version etc. No matter why you want to do that you have a few options to achieve the goal.
The first approach is building a factory method that will add the required header.
func newRequest(endpoint string) *http.Request {
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s", endpoint), nil)
req.Header.Add("x-api-key", "my-secret-token")
return req
}It’s very simple and clear but it requires you to create a new method per HTTP method or calling another function directly. You’ll have to do it every time you create a new request.
When I started my career as a software developer and published the first production application what I did was staring at logs and look for some fatal errors. It was a monolith application. Every log saying that something’s wrong had to be fixed. ASAP. This approach worked for some time. However, when the scale increased, and I started building microservices, I couldn’t get rid of all of them. Network issues, database failures, and more - it happens all the time.
In Go, we can refer to variables using value or pointers. Sometimes, it’s hard to answer which approach is more suitable.
At the first place, you should learn about general rules. Value semantic should be used every time when copying the value make sense in the logic of your code. For example, every value object should be passed by value. If you have a struct Money then it’s possible (and also make sense) to have, at the same time, multiple 10$ in your code. There’s no such requirement that only one person can have the same amount of money.
Slice is the most important data structure in Go. When it comes to performance, slices are going to beat any other data structure. They are simple but powerful. However, there are some gotchas you have to keep in mind. Today, I’ll explain how slices work to help you prevent some hard to find bugs and write better code.
In Go, arrays have a fixed size. The length is part of the array’s type. It means that two arrays of integers but with different sizes are different types.