Writing custom linter in Go
Writing linters is simple. I was surprised how it’s easy to write a Go linter. Today, we’ll write a linter that will calculate the cyclomatic complexity of the Go code. What is cyclomatic complexity? Cyclomatic complexity is a software metric used to indicate the complexity of a program. ref The idea is simple - every time we find any control flow statements we increase the complexity by one. I know I oversimplified it a bit but I don’t want to overwhelm you with unnecessary details.
Is my interface too big?
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.
Go with some context
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.
Go web frameworks
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.
Pointer and value semantics in Go
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.
What you should know about Go slices
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.
In the garbage-collected world, we want to keep the GC overhead as little as possible. One of the things we can do is limiting the number of allocations in our application. How to achieve that? There’s sync.Pool which caches allocated but unused items for later reuse. The Pool can become very useful when you have multiple parallel operations that can share the same piece of memory between them. The real power of it is visible when you have frequent allocations and deallocations of the same data structure.
OAuth2 and Go
OAuth 2.0 is the industry-standard protocol for authorization. Go has built-in support for this protocol and today we’ll build a simple application. The application will use the Facebook API to authorize a user. If you need to clarify what oauth2 is and how it works you can take a look at the introduction from DigitalOcean. There are some videos as well. The very first step of building our program is creating a new Facebook application.
Garnish - simple varnish implementation written in Go
The varnish is a well-known HTTP accelerator. As the continuation of the GoInPractice series, today I’ll show how you can build a simple (and naive) varnish implementation in Go. Some of the code is reused from Writing a reverse proxy so if you don’t understand something, I recommend taking a look at the blog post. We’ll split our project into a few parts. The first one will be the caching mechanism.
I want to learn Go - how to start?
You can find a lot of materials about Go (including this blog) but it’s hard to find the best place to start. This article’s goal is to sum up the most valuable materials I found to help others. I focus only on free materials. Fundamentals The Go tour - IMO the absolutely must do. You can try Go without installing it. You’ll learn some basic syntax and concepts step by step, Go by example - if you’re confused how to use a certain part of the language it’s possible you’ll find an example of it on this page.