Implementing a Kafka Producer and Consumer In Golang (With Full Examples) For Production

In this post we will learn how to create a Kafka producer and consumer in Go. We will also look at how to tune some configuration options to make our application production-ready. Kafka is an open-source event streaming platform, used for publishing and processing events at high-throughput. There are a lot of popular libraries for Go in order to interface with Kafka. For this post, we will be using the kafka-go library (but the same concepts will apply for any other library as well)....

 · Soham Kamani

Implementing RSA Encryption and Signing in Golang (With Examples)

This post will describe what the RSA algorithm does, and how we can implement it in Go. RSA (Rivest–Shamir–Adleman) encryption is one of the most widely used algorithms for secure data encryption. It is an asymmetric encryption algorithm, which is just another way to say “one-way”. In this case, it’s easy for anyone to encrypt a piece of data, but only possible for someone with the correct “key” to decrypt it....

 · Soham Kamani

Type Assertions vs Type Conversions in Golang

Type assertions and type conversions seem to be a confusing topic in Go, since they both seem to do the same thing. In this article, we will see how assertions and conversions are actually quite different, and go under the hood to see what happens when you use each of them in Go. First, let’s see what they look like… This is a type assertion in Go: var greeting interface{} = "hello world" greetingStr := greeting....

 · Soham Kamani

A Guide On SQL Database Transactions In Go

This article explains what SQL database transactions are, and how to implement them in Go (Golang). Transactions are very useful when you want to perform multiple operations on a database, but still treat them as a single unit. We will go into detail on why this is useful, and how you can use it in your Go applications. To illustrate our examples, we will use Postgres as our database of choice....

 · Soham Kamani

Making an interactive Telegram bot in Go (Golang)

This tutorial will go through a straightforward set of steps to get a responsive telegram bot up and running from scratch in Golang I spent a considerable amount of time figuring out how to make a functional telegram bot. I mean sure, the official introduction is good, but theres a lot of stuff about what bots are, and a few scattered instructions about the API, but not enough of structure for a beginner to get up and running quickly....

 · Soham Kamani

Functional Options in Go: Implementing the Options Pattern in Golang

This post talks about what functional options are in Go, and how we can use the options pattern to implement them. Functional options take the form of extra arguments to a function, that extend or modify its behavior. Here’s an example which uses functional options to create a new House struct: h := NewHouse( WithConcrete(), WithoutFireplace(), ) Here, NewHouse is a constructor function. WithConcrete and WithFireplace are options passed to the constructor to modify it’s return value....

 · Soham Kamani

Command Pattern in Go (Golang)

This article will explain the command pattern, where to use it, and how to implement it in Go. The command pattern, as the name suggests, is used when we want to create and execute “commands”. Different commands have their own implementation, but have the same steps for execution. The Command Interface The basic unit for implementing the command pattern is the Command interface: type Command interface { execute() } If the command can error out, the interface can contain an error return value as well:...

 · Soham Kamani

A Comprehensive Guide of Arrays and Slices in Golang (and their differences)

At first, it’s easy to see arrays and slices as the same thing: a data structure to represent collections. However, they are actually quite different from one another. In this post we will explore their differences and implementation in Go. We will then look at some examples so that you can make a more informed decision on when to use them. Arrays An array is a fixed collection of data. The emphasis here is on fixed, because once you set the length of an array, it cannot be changed....

 · Soham Kamani

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