How to Create Constant Maps, Slices, & Arrays in Golang

For the most part, I’ve noticed that Go developers are pretty good about using global constants for configuration, rather than global variables. A problem arises however when we want a constant slice, array, or map. The Go compiler doesn’t allow these more complex types to be set as constant. Many developers, upon making this realization, decide to then use a dangerous global variable. In this article, we will explore a better option for declaring a Golang const map, slice, or array.

If instead, you’re interested in doing a deeper dive into Go’s quirks checkout our Go Mastery course that covers all this material in greater depth.

A Brief Refresher on Globals and Constants

package foo

// this is a global constant
const safeRateLimit = 10

// this is a global variable
var dangerousRateLimit = 10

When setting configuration globals, which should be read-only, there is no reason to use a global variable. By using a variable instead of a constant you:

  • Open up the potential for bugs when you or someone else accidentally mutates the value
  • Confuse future developers who assume the value is supposed to change

Most people already know this about global variables thankfully, and switching global variables to global constants is a fairly straightforward task.

What If I Want A Global Array, Map, or Slice?

global slice

Let’s assume the following situation:

We have a program that needs two sets of configurations. The configurations are:

  • A list of supported social media networks
  • A rate limit for making API calls to the networks (we assume they all have the same rate limit)

Now that we know a bit about the configurations we make the following decisions:

  • Because these configurations will not change based on the environment the program is running in, we elect to set the values in code rather than using environment variables
  • Since they are needed in many places in the app, we choose to scope them globally, instead of passing them into 20+ functions
  • Because they should not change during the execution of the program, we decide to make them constant

We then write the following code:

package main

const rateLimit = 10

const supportedNetworks = []string{"facebook", "twitter", "instagram"}

Much to our surprise, when we try to compile this code we get the following error:

const initializer []string literal is not a constant

Unlike constants in JavaScript, Go doesn’t allow complex types like slices, maps, or arrays to be constant! Our first instinct may be to lazily switch it to a variable, and add a comment:

package main

const rateLimit = 10

// this is meant to be constant! Please don't mutate it!
var supportedNetworks = []string{"facebook", "twitter", "instagram"}

Whenever we find ourselves leaving comments like this, we should be aware we are doing something wrong.

The Better Solution for Constants in Go

It’s much better to use an initializer function (not to be confused with Go’s conventional init() function). An initializer function is a function that simply declares something and returns it. A good solution to our problem would be as follows:

package main

const rateLimit = 10

func getSupportedNetworks() []string {
	return []string{"facebook", "twitter", "instagram"}

Now, anywhere in the program, we can use the result of getSupportedNetworks() and we know that there is no way we can get a mutated value.

We can use a similar pattern for a map:

func getSupportedNetworkRatelimits() map[string]int {
	return map[string]int{
		"facebook":  10,
		"twitter":   50,
		"instagram": 60,

Good Practices

Being able to keep access to maps and slices that are effectively constant can make your code easier to read, and more importantly, much less error-prone. One the most-sought after traits of a computer scientist for high-end coding jobs is the ability to read, write, and refactor code so that it’s more maintainable and easier to understand.

Thanks For Reading!

Take computer science courses on our new platform

Follow and hit us up on Twitter @q_vault if you have any questions or comments

Subscribe to our Newsletter for more programming articles

Related Qvault Reading

%d bloggers like this: