Go blueprints: code for common tasks
These code examples are intended to help you quickly solve some common everyday tasks in Go. There are also a few oddities that may be nice to have when writing more exotic code.
2 basic FIFO queue implementations
How to implement a FIFO queue in Go. Use a slice for a temporary queue. For a long-living queue you should probably use a dynamic data structure, such as a linked list.
2 basic set implementations
How to implement a hashset in Go using built-in maps with boolean or empty struct values.
A basic stack (LIFO) implementation
How to implement a stack or LIFO queue in Go using a slice and the append function.
Access environment variables
Use the Setenv, Getenv, Unsetenv and Environ functions to read and write environment variables.
Access private fields with reflection
How to read unexported fields in a struct using reflection in Go.
Bitmasks and flags
A bitmask is a small set of booleans, often called flags, represented by the bits in a single number.
Check if a number is prime
To check if a number is prime in Go use the ProbablyPrime function from package math/big.
Go as a scripting language: lightweight, safe and fast
How to write a basic command-line (CLI) application in Go.
You can access command-line arguments passed to a Go program through the os.Args variable.
Compute absolute value of an int/float
Write your own code to compute the absolute value of an integer, but use math.Abs for floating point numbers.
Compute max of two ints/floats
Write your own code to compute the minimum and maximum of integers, but use math.Min and math.Max for floating point numbers.
Format byte size as kilobytes, megabytes, gigabytes, ...
Utility functions for converting byte size to human-readable format. The code supports both SI and IEC formats.
3 simple ways to create an error
How to create simple string-based errors and custom error types with data.
Create a new image
To generate a png image programmatically in Go use the image, image/color, and image/png packages.
Enum with String function
How to declare an enumeration and give it a string representation.
Find build version
Use runtime.Version to find the current Go build version.
Generate all permutations
How to generate all permutations of a slice or string in Go.
Hash checksums: MD5, SHA-1, SHA-256
To compute the hash value of a string or byte slice, use the Sum function from crypto package md5, sha1 or sha256. For a file or input stream you need to create a Hash object and write to its Writer function.
HTTP server example
How to implement a basic HTTP web server in Go with client requests and responses.
How to iterate in Go
Iterator pattern: How to write iterators and generators in Go.
Maximum value of an int
The max and min values of an int can be computed as untyped constants.
Round float to 2 decimal places
How to round a float to a string/float with 2 decimal places in Go.
Round float to integer value
How to round a float64 to the nearest integer: round away from zero, round to even number, convert to an int type.
Table-driven unit tests
How to write a table-driven unit test for binary search in Go.
The 3 ways to sort in Go
How to sort any type of data in Go using the sort package. All algorithms in the package perform O(n log n) comparisons in the worst case.
See the String handling cheat sheet for all about basic string manipulation in Go.
Follow on Twitter
One useful golang fact per day
Share this page: