Understanding Golang Generics with Examples

Yash Prakash
This Code
Published in
4 min readOct 27, 2022

--

Easy tutorial for using generic types and functions in Go

Photo by Marcello Gennari on Unsplash

In Golang version 1.18, we were introduced to a new feature called generic types in the language. With generics, we can declare and use functions or types that are written to work with any type of a set of types provided.

Simply, your generic functions and types will work with any number of types that you define them for, hence making them more useful in a broader sense in your Go program or project as a whole.

Let’s see what they’re all about in detail and how to utilize them effectively in our code.

Prerequisites

To follow this tutorial, you will need:

  • Go version 1.18 or greater installed. To set this up, follow my previous tutorial on installing and using common development utilities for Golang:

A simple use case

Imagine that you are in need of a function that can take the following kinds of inputs:

  1. A set of key-value pairs of…

--

--

Yash Prakash
This Code

Software engineer → Solopreneur ⦿ Scaling my own 1-person business model ⦿ Writing for busy founders and business owners.