What is a context? Context is a package in the standard library which is mainly used to propagate cancellation signals from one function to another or even from micro service to another. Let’s consider the example of a user sending a get request to a web server to download 50 images, zip it and send the zipped response back. The request has been triggered from the browser and let’s say…
Welcome to tutorial no. 1 in our MySQL tutorial series. In this tutorial, we will connect to MySQL and create a database. We will also ping the DB to ensure that the connection is established properly. MySQL Series Index Connecting to MySQL and creating a Database Creating a Table and Inserting Rows Selecting single and multiple rows Prepared statements - WIP Updating rows - WIP Deleting rows -…
Welcome to tutorial no. 2 in our MySQL tutorial series. In the first tutorial, we discussed how to connect to MySQL and create a database. In this tutorial, we will learn how to create a table and insert records into that table. MySQL Series Index Connecting to MySQL and creating a Database Creating a Table and Inserting Rows Selecting single and multiple rows Prepared statements - WIP Updating…
Welcome to tutorial no. 3 in our MySQL tutorial series. In the previous tutorial, we discussed creating a table and inserting rows into the table. In this tutorial, we will learn how to select a single row as well as multiple rows from a table. MySQL Series Index Connecting to MySQL and creating a Database Creating a Table and Inserting Rows Selecting Single Row and Multiple rows Prepared…
Although Docker and container orchestrators like Kubernetes have solved the issues such as ensuring that a web service keeps running all the time, they are quite complicated to configure and maintain for small SAAS applications and side projects. For my own side projects such as https://sslnotify.com/, I use a systemd service to deploy my Go binary. This setup is very simple and it serves my…
Admission Controllers Admission controllers provide a mechanism to validate or modify Kubernetes object creation requests before the object is actually created. This definition might not make complete sense right now but we will be able to understand what this means as the tutorial progresses, so don’t worry about it :). There are two kinds of admission controllers in Kubernetes. Validating…
Welcome to tutorial no. 17 in Golang tutorial series. Introduction A method is just a function with a special receiver type between the func keyword and the method name. The receiver can either be a struct type or non-struct type. The syntax of a method declaration is provided below. func (t Type) methodName(parameter list) { } The above snippet creates a method named methodName with receiver type…
Welcome to tutorial no. 37 in Golang tutorial series. In this tutorial, we will learn how to write data to files using Go. We will also learn how to write to a file concurrently. This tutorial has the following sections Writing string to a file Writing bytes to a file Writing data to a file line by line Appending to a file Writing to a file concurrently Please run all the programs of this tutorial…
Welcome to tutorial no. 36 in Golang tutorial series. File reading is one of the most common operations performed in any programming language. In this tutorial, we will learn about how files can be read using Go. This tutorial has the following sections. Reading an entire file into memory Using an absolute file path Passing the file path as a command line flag Bundling the file inside the binary…
Welcome to tutorial no. 15 in Golang tutorial series. In this tutorial, we will learn how pointers work in Go and we will also understand how Go pointers differ from pointers in other languages such as C and C++. This tutorial has the following sections. What is a pointer? Declaring pointers Zero value of a pointer Creating pointers using the new function Dereferencing a pointer Passing pointer to…
Welcome to tutorial no. 35 in Golang tutorial series. Reflection is one of the advanced topics in Go. I will try to make it as simple as possible. This tutorial has the following sections. What is reflection? What is the need to inspect a variable and find its type? reflect package reflect.Type and reflect.Value reflect.Kind NumField() and Field() methods Int() and String() methods Complete…
Welcome to tutorial no. 34 in Golang tutorial series. What are first class functions? A language that supports first class functions allows functions to be assigned to variables, passed as arguments to other functions and returned from other functions. Go has support for first class functions. In this tutorial, we will discuss the syntax and various use cases of first class functions. Anonymous…
Welcome to tutorial no. 31 in our Golang tutorial series. In the last tutorial we learnt about error representation in Go and how to handle errors from the standard library. We also learnt how to extract more information from the errors. This tutorial deals with how to create our own custom errors which we can use in our functions and packages. We will also use the same techniques employed by the…
Welcome to tutorial no. 30 in Golang tutorial series. What are errors? Errors indicate any abnormal condition occurring in the program. Let’s say we are trying to open a file and the file does not exist in the file system. This is an abnormal condition and it’s represented as an error. Errors in Go are plain old values. Just like any other built-in type such as int, float64, ……
Welcome to tutorial no. 28 in Golang tutorial series. Polymorphism in Go is achieved with the help of interfaces. As we have already discussed, interfaces are implicitly implemented in Go. A type implements an interface if it provides definitions for all the methods declared in the interface. Let’s see how polymorphism is achieved in Go with the help of interfaces. Polymorphism using an…
Welcome to tutorial no. 27 in Golang tutorial series. Go does not support inheritance, however, it does support composition. The generic definition of composition is “put together”. One example of composition is a car. A car is composed of wheels, an engine and various other parts. Composition by embedding structs Composition can be achieved in Go is by embedding one struct type into…
Welcome to tutorial no. 20 in Golang tutorial series. Go is a concurrent language and not a parallel one. Before discussing how concurrency is taken care in Go, we must first understand what is concurrency and how it is different from parallelism. What is concurrency? Concurrency is the capability to deal with lots of things at once. It’s best explained with an example. Let’s consider…
Welcome to tutorial no. 25 in Golang tutorial series. In this tutorial, we will learn about mutexes. We will also learn how to solve race conditions using mutexes and channels. Critical section Before jumping to mutex, it is important to understand the concept of critical section in concurrent programming. When a program runs concurrently, the parts of code which modify shared resources should not…
Welcome to tutorial no. 24 in Golang tutorial series. What is select? The select statement is used to choose from multiple send/receive channel operations. The select statement blocks until one of the send/receive operations is ready. If multiple operations are ready, one of them is chosen at random. The syntax is similar to switch except that each of the case statements will be a channel…
Welcome to tutorial no. 23 in Golang tutorial series. What are buffered channels? All the channels we discussed in the previous tutorial were basically unbuffered. As we discussed in the channels tutorial in detail, sends and receives to an unbuffered channel are blocking. It is possible to create a channel with a buffer. Sends to a buffered channel are blocked only when the buffer is full.…
This is the first tutorial in our Golang tutorial series. This tutorial provides an introduction to Go and also discusses the advantages of choosing Go over other programming languages. We will also learn how to install Go in Mac OS, Windows and Linux. Introduction Go also known as Golang is an open source, compiled and statically typed programming language developed by Google. The key people…
What are WebSockets and what are their advantages over traditional HTTP? HTTP is simply a request response protocol. This means a client such as a web browser sends a request and the server works on this request and sends a response. This request and response uses a TCP connection underneath. Once the response is received, the TCP connection is closed and can’t be reused. The client has to…
Welcome to tutorial no. 21 in Golang tutorial series. In the previous tutorial, we discussed concurrency and how it is different from parallelism. In this tutorial, we will discuss how concurrency is achieved in Go using Goroutines. What are Goroutines? Goroutines are functions or methods that run concurrently with other functions or methods. Goroutines can be thought of as lightweight threads.…
Welcome to tutorial no. 22 in Golang tutorial series. In the previous tutorial, we discussed about how concurrency is achieved in Go using Goroutines. In this tutorial we will discuss about channels and how Goroutines communicate using channels. What are channels Channels can be thought of as pipes using which Goroutines communicate. Similar to how water flows from one end to another in a pipe,…
Welcome to tutorial no. 33 in Golang tutorial series. What is Panic? The idiomatic way of handling abnormal conditions in a Go program is using errors. Errors are sufficient for most of the abnormal conditions arising in the program. But there are some situations where the program cannot continue execution after an abnormal condition. In this case, we use panic to prematurely terminate the…
My GopherCon UK 2025 talk Climbing the Testing Pyramid: From Real Service to Interface Mocks in Go can be watched on youtube. This is the first time I am speaking at GopherCon and also in front of a large audience of nearly 150 people. This talk explores the various strategies for unit testing Go applications that interact with cloud services like S3. Testing against real cloud services is…
Welcome to tutorial no. 16 in our Golang tutorial series. What is a struct? A struct is a user-defined type that represents a collection of fields. It can be used in places where it makes sense to group the data into a single unit rather than having each of them as separate variables. For instance, an employee has a firstName, lastName and age. It makes sense to group these three properties into a…
Welcome to tutorial no. 14 in Golang tutorial series. Strings deserve a special mention in Go as they are different in implementation when compared to other languages. What is a String? A string is a slice of bytes in Go. Strings can be created by enclosing a set of characters inside double quotes " ". Let’s look at a simple example that creates a string and prints it. 1package main 2…
Welcome to part 12 of our Golang tutorial series. What is a variadic function? Functions in general accept only a fixed number of arguments. A variadic function is a function that accepts a variable number of arguments. If the last parameter of a function definition is prefixed by ellipsis …, then the function can accept any number of arguments for the last parameter. Only the last…
Welcome to the part 11 of Golang tutorial series. In this tutorial, we will learn about Arrays and Slices in Go. What is an Array? An array is a collection of elements. For example, the collection of integers 5, 8, 9, 79, 76 constitute an array. Declaration An array belongs to the type [n]T. n represents the number of elements in an array and T represents the type of each element.
Why mocks are needed? I generally prefer testing my code without using mocks. For example, if the code is integrated with Elastic Search, I prefer to run a elastic search container locally and test the Go code against that container. But there are some cases where creating a local environment for testing is not possible. This usually happens when integrating with cloud providers. Let’s take…
Go 1.24 introduced a new json struct tag called omitzero. It handles a couple of scenarios that the existing omitempty tag couldn’t. Let’s look at them one by one. Marshalling empty time.Time struct fields Let’s say we have the following customer struct 1type customer struct { 2 Name string `json:'name,omitempty'` 3 DOB time.Time `json:'dob,omitempty'` 4} We have added the…
Welcome to tutorial no. 19 in Golang tutorial series. This is the second part in our 2 part interface tutorial. In case you missed the first part, you can read it here interfaces-part-1. Implementing interfaces using pointer receivers vs value receivers All the interfaces we discussed in part 1 were implemented using value receivers. It is also possible to implement interfaces using pointer…
Welcome to tutorial no. 18 in Golang tutorial series. This is the first part in our 2 part interface tutorial. What is an interface? In Go, an interface is a set of method signatures. When a type provides definition for all the methods in the interface, it is said to implement the interface. Interface specifies what methods a type should have and the type decides how to implement these methods.
This is tutorial number 10 in Golang tutorial series. What is a switch statement? A switch is a conditional statement that evaluates an expression and compares it against a list of possible matches and executes the corresponding block of code. It can be considered as an idiomatic way of replacing complex if else clauses. Example An example program is worth a hundred words. Let’s start with a…
Welcome to tutorial number 9 in Golang tutorial series. A loop is used to execute a block of code repeatedly until a condition is satisfied. for is the only loop available in Go. Go doesn’t have while or do while loops which are present in other languages like C. for loop syntax for initialisation; condition; post { } The initialisation statement will be executed only once. After the loop is…
Welcome to tutorial no. 6 in our Golang tutorial series. What is a function? A function is a block of code that performs a specific task. A function takes an input, performs some operations on the input and generates outputs. For example, a function can take the radius as input and calculate the area and circumference as output. Function declaration The following is the syntax for declaring a…
Welcome to tutorial no. 13 in Golang tutorial series. What is a map? A map is a inbuilt data type in Go which is used to store key-value pairs. A practical use case for a map is for storing the currency codes and the corresponding currency names. USD - United States Dollar EUR - Euro INR - India Rupee A map will be a perfect fit for the above use case use case.
Welcome to tutorial no. 29 in Golang tutorial series. What is Defer? Defer statement is used to execute a function call just before the surrounding function where the defer statement is present returns. The definition might seem complex but it’s pretty simple to understand by means of an example. Example 1package main 2 3import ( 4 'fmt' 5 'time' 6) 7 8func totalTime(start time.Time) { 9…
Welcome to tutorial number 8 of our Golang tutorial series. if statement has a condition and it executes a block of code if that condition evaluates to true. It executes an alternate else block if the condition evaluates to false. In this tutorial we will look at the various syntaxes for using a if statement. If statement syntax The syntax of the if statement is provided below if condition { } If…
Welcome to tutorial no. 5 in our Golang tutorial series. What is a constant? Constants in Go is used to denote fixed static values such as 95 'I love Go' 67.89 and so on. Constants are generally used to represent values that do not change throughout the life time of an application. Declaring a constant The keyword const is used to declare a constant in Go. Let’s see how to declare a constant…
Welcome to tutorial number 4 in our Golang tutorial series. Please read Golang tutorial part 3: Variables of this series to learn about variables. The following are the basic data types available in Go bool Numeric Types int8, int16, int32, int64, int uint8, uint16, uint32, uint64, uint float32, float64 complex64, complex128 byte rune string bool bool type represents a boolean. It can either be a…
This is the third tutorial in our Golang tutorial series and it deals with variables in Golang. You can read part 2 to learn about configuring Go and running the hello world program. What is a variable? Variable is the name given to a memory location to store a value of a specific type. There are various syntaxes to declare variables in Go. Let’s look at them one by one.
Welcome to tutorial number 7 in Golang tutorial series. What are packages and why are they used? So far we have seen Go programs that have only one file with a main function and a couple of other functions. In real-world scenarios, this approach of writing all source code in a single file is not scalable. It becomes impossible to reuse and maintain code written this way. This is where packages are…
This is the second tutorial in our Golang tutorial series. Please read our previous tutorial Golang Introduction and Installation to know about what is Golang and how to install Golang. There is no better way to learn a programming language than getting our hands dirty with code. Let’s go ahead and write our first Go program. Setting up the development environment Let’s create a…
Welcome to tutorial no. 2 of our WebAssembly tutorial series. Series Index Introduction to WebAssembly Using Go Accessing DOM from Go using Javascript In the first tutorial of this tutorial series, we created and exposed a function from Go and called it using JavaScript. I highly recommend reading the first part first part if you have not read it yet. In this tutorial, we will develop a UI for our…
Welcome to tutorial no. 1 of our WebAssembly tutorial series. Series Index Introduction to WebAssembly Using Go DOM Access and Error Handling What is WebAssembly? JavaScript has been the only programming language that the browser understands. JavaScript has stood the test of time and it has been able to deliver the performance needed by most web applications. But when it comes to 3D games, VR, AR,…
Why migrate to Hugo? Why migrate golangbot.com from Ghost to Hugo is the first question that will pop in your mind when you read this post. golangbot.com is 6 years old and it has been powered by Ghost since its inception. Ghost has stood the test of time and it has served me well. Why migrate to Hugo then? Well I have my own reasons. Ghost doesn’t support SQLite anymore Ghost has been…
Need for a debugger The simplest form of debugging in any programming language is by using print statements/logs and writing to standard out. This definitely works but becomes extremely difficult when the size of our application grows and the logic becomes more complex. Adding print statements to every code path of the application is not easy. This is where debuggers come in handy. Debuggers help…
Welcome to tutorial no. 32 in our Golang tutorial series. In this tutorial we will learn about error wrapping in Go and why do we even need error wrapping. Let’s get started. What is error wrapping? Error wrapping is the process of encapsulating one error into another. Let’s say we have a web server which accesses a database and tries to fetch a record from the DB. If the database call…