Golang functions vs methods 👯‍♀️

This post will describe the key differences between functions and methods in Go, and when it’s best to use them. Functions and methods are both used extensively in Go to provide abstractions and make our programs easier to read and reason about. On the surface, functions and methods both look similar, but there are some important semantic differences which can make a big difference to the readability of your code....

 · Soham Kamani

Polymorphism in Go - without interfaces 🦆

Ok, so you want to use polymorphism in Go, but don’t like interfaces? Read on… First, let’s see what we want to do: var dog, duck *Animal dog = NewDog("fido") duck = NewDuck("donald") fmt.Println(dog.makeNoise()) // fido says woof! fmt.Println(duck.makeNoise()) // donald says quack! Here, dog and duck are of the same type (*Animal). Each variable is instantiated with a different constructor, and they have different behaviours when the same method makeNoise is called....

 · Soham Kamani

Implementing JWT based authentication in Golang

In this post, we will learn how JWT(JSON Web Token) based authentication works, and how to build a server application in Go to implement it using the golang-jwt/jwt library. If you already know how JWT works, and just want to see the implementation, you can skip ahead, or see the source code on Github The JSON web token (JWT) allows you to authenticate your users in a stateless manner, without actually storing any information about them on the system itself (as opposed to session based authentication)....

Last Updated:   · Soham Kamani

Golang Omitempty Explained

If you’ve worked with Go for sometime, you have probably encountered the omitempty tag, when trying to encode a struct to JSON. This post will explain what exactly the omitempty tag means, and some common pitfalls, along with ways to get around them. Basic Usage When using Go to build web applications, or HTTP servers, there will eventually come a time when we need to encode some data as JSON. The problem here is that the JSON representation does not exactly match the way Go handles data....

 · Soham Kamani

Creating an OAuth2 Client in Golang (With Full Examples)

In this post we will see how we can implement OAuth2 authentication in a Go web application. We will create a working website that can allow a user to sign in using Github authentication. If you just want to see the code, you can view the full example on Github How OAuth2 Works Let’s take a brief look at the OAuth protocol before we jump into implementation. If you’ve ever seen a dialog like this, then you’ve probably used OAuth before:...

Last Updated:   · Soham Kamani

Factory patterns in Go (Golang) 🏭

The factory pattern is a commonly used pattern in object oriented programming. In Go, there are many different ways in which you can use the factory pattern to make your code cleaner and more concise. Go’s answer to classes, are structs. For example, the struct for a “Person”, along with a “Greet” method would look like this: type Person struct { Name string Age int } func (p Person) Greet() { fmt....

 · Soham Kamani

Using Context in Golang - Cancellation, Timeouts and Values (With Examples)

In this post, we’ll learn about Go’s context package, and more specifically, how we can use cancellation to improve our applications performance. We will also go through some patterns and best practices when using the context package in your Golang application. When Do We Use Context? As the name suggests, we use the context package whenever we want to pass around “context”, or common scoped data within our application. For example:...

Last Updated:   · Soham Kamani

How to generate an error stack trace in Go 🥞

A common problem that many people (including me) have faced when programming in Go, is to pin point the source of an error. Other programming languages provide you with a stack trace to tell you where the error came from, but Go does not have this behavior by default. In this article, we will discuss how to use the fmt, and the github.com/pkg/errors libraries to give us better error reporting....

 · Soham Kamani

Session Cookie Authentication in Golang (With Complete Examples)

In this post, we will learn how to authenticate users using session cookies in a Golang server application. When a user logs into our application, we need to know who they are across all our HTTP methods and routes. One way to do this is to store the users “session”. A session is started once a user logs in, and expires some time after that. Each logged in user has some reference to the session (called a cookie), which they send with their requests....

Last Updated:   · Soham Kamani

How to Implement Password Authentication and Storage in Go (Golang)

This post will explain how to sign up and login users using password based authentication in Go. Any application that stores passwords has to ensure that its passwords are stored securely. You can’t just store passwords as plain text, and it should ideally be impossible to guess your users passwords, even if you have access to their data. In this post, we will explain the production-ready method for storing passwords, and build a web application in Go to demonstrate it in practice....

Last Updated:   · Soham Kamani