Category Golang

What you should know about Go slices
9 min read

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.

Using sync.Pool
5 min read

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. The Pool scales up when the load increases and can be cleared in every 2 GC circles. I’ll explain the algorithm behind it a bit later. Right now, I focus on how to use it.

OAuth2 and Go
3 min read

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. You can do it in Facebook for Developers. Please remember to choose the platform Website while creating. What’s more, any other application on Twitter or LinkedIn will be fine as well. I use Facebook as an example.

Garnish - simple varnish implementation written in Go
3 min read

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. Its responsibility will be storing the data to cache and invalidate it after reaching the deadline. We’ll use mutexes for synchronization between goroutines.

I want to learn Go - how to start?
3 min read

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. Extremely usefull.
  • https://golangnews.org/ and http://www.go-gazette.com/ newsletters - it’s a great source of good quality materials related to Go,
  • 50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs - a list of “gotchas” in Go. It’s a long list and there’s now way you’ll remember everything but it’s a good reference for future.
  • How to Write Go Code - some people recommend it but actually I don’t. It uses GOPATH and doesn’t use go modules what makes it a bit outdated.
  • Golang Tutorial for Beginners [video] - very basic introduction to the language but with quite good explenation of some context you may not catch while finishing The Go tour. Recommended as follow-up.
  • Learn Go in 12 Minutes [video] - covers the very basic Go aspects. You definitely won’t know how to write Go code after watching it but it’s a good demo of the syntax and a few Go concepts.

After that…

  • Effective Go - it’s a list of tips for writing clear, idiomatic Go code. It’s the gold standard and definitely must read.
  • Go Code Review Comments - as authors say - it’s a suplement to Effective Go. It’s an official list of common mistakes with explanation.
  • Go blog - if you want to be up-to-date with changes to the language and its tools
  • Ardan labs blog - one of the best Go blogs. You’ll find a series of post about the GC or go modules and much more.

Books

  • The Little Go Book - a bit old book which covers fundamental aspects of Go. It doesn’t contain anything special. I’d rather think about it like the supplement of The Go tour.
  • An Introduction to Programming in Go - much more complex book which tells you more about concurrency or testing (veeery basic). It contains simple “problems” to solve which will help you understand those aspects better.
  • Go Bootcamp - basicaly it covers the same topics as the book above but in more details. It contains links to external resources like Rob’s talk what’s a huge plus.
  • Webapps in Go - If you want to learn how to write a web app - the book is for you. You’ll find there how to connect to a DB, how to work with forms, upload a file etc.
  • Test-driven development with Go - writing tests in prev materials isn’t covered very well. In some cases, only in a few sentences. Unfortunatelly, it’s not finished yet.
  • Build web application with golang - a book translated to many languages where you’ll learn how to build a web application in Go using beego.

As you may guess, the best teacher is practice! I hope that the list will help you finding useful materials and will speed your learning up! If you know any other cool materials, let me know in the comments section below. Cheers!

Writing a reverse proxy in Go
2 min read

Some time ago, I found a video called Building a DIY proxy with the net package. I recommend watching it. Filippo Valsorda builds a simple proxy using low-level packages. It’s fun to watch it but I think it’s a bit complicated. In Go, it has to be an easier way so I decided to continue writing series Go In Practice by writing a simple but yet powerful reverse proxy as fast as it’s possible.

Writing TCP scanner in Go
4 min read

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. If the client gets a timeout here it may mean that the port is behind a firewall.

Golang Tips & Tricks #7 - private repository and proxy
1 min read

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.

How I organize packages in Go
4 min read

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.

I mostly develop microservices and this architecture fits great to my needs. Projects with much more domain in it or more infrastructure applications may require a different approach. Let me know in the comments below what’s your design and where it makes sense the most.

Golang Tips & Tricks #6 - the _test package
1 min read

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.