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

Data races in Go(Golang) and how to fix them

Go is known for how easy it is to build concurrent programs in it. But, with all this concurrency, comes the possibility of the dreaded data race – one of the hardest bugs to debug if you’re ever unfortunate enough to encounter it in your code. In this post, we will go through a sample program that causes a data race, and detect the race condition with the race detector tool....

 · Soham Kamani

A Complete Guide to JSON in Golang (With Examples)

In this post, we will learn how to work with JSON in Go, in the simplest way possible. We will learn how to convert from JSON raw data (strings or bytes) into Go types like structs, arrays, and slices, as well as unstructured data like maps and empty interfaces. JSON is used as the de-facto standard for data serialization, and by the end of this post, you’ll get familiar with how to marshal (encode) and unmarshal (decode) JSON in Go...

Last Updated:   · Soham Kamani

Build a web application in Go (golang)

Go is getting more and more popular as the go-to language to build web applications. This is in no small part due to its speed and application performance, as well as its portability. There are many resources on the internet to teach you how to build end to end web applications in Go, but for the most part they are either scattered in the form of isolated blog posts, or get into too much detail in the form of books....

Last Updated:   · Soham Kamani

An Introduction to Channels in Go (Golang)

This post highlights how channels work in Go, and how you can use them in your code. In Go, a channel is a programming construct that allows us to move data between different parts of our code, often from different goroutines. Creating Channels We can declare a new channel type by using the chan keyword along with a datatype: var c chan int Here, c is of type chan int - which means it’s a channel through which int types are sent....

Last Updated:   · Soham Kamani