RSVP here for our first hackathon! Free prizes, the kickoff is 8 AM PST Jan 17th

Make Maps and Slices in Golang – A Guide to Initialization

Learn the coding skills you need for your next job in tech

  1. Join the Qvault community and share your career goals
  2. We'll help you find the knowledge gaps holding you back
  3. Complete recommended courses and projects
  4. Find your next opportunity with a newly polished resume

Our courses include but are not limited to

  • Golang, Python, JavaScript
  • Algorithms, data structures, cryptography
  • Graphics and functional programming

There are quite a few ways to create new maps and slices in Go, for example, they can both be initialized using the make() function, the new() function, as literals, or by using the var keyword. With so many choices, which option is best? Or perhaps better asked, which one is best in your situation? Let’s take a look.

Slices

var varStyle []string literalStyle := []string{} newStyle := new([]string) makeStyle := make([]string, 0)
Code language: Go (go)

var varStyle []string is the idiomatic way to declare an empty slice. The slice is actually nil, which means it will be null when marshalled to JSON and will succeed nil checks.

literalStyle := []string{} should probably only be used when the literal is going to start with values in it, as in literalStyle := []string{"cat", "dog", etc}. Otherwise prefer make()

newStyle := new([]string) returns a pointer to the slice. Same as ptrStyle := &[]string{}. Only use if you want a pointer.

makeStyle := make([]string, 0) is the same as the literal style, but is preferred for idiomatic reasons when the slice doesn’t need non-zero starting values. Make() allows the slice to be initialized with a starting length and capacity, which can have good performance implications in some circumstances:

makeStyle := make([]string, len, cap)

Maps

var varStyle map[int]int literalStyle = map[string]int{} newStyle := new(map[string]int) makeStyle := make(map[string]int)
Code language: Go (go)

var varStyle map[int]int creates a nil map. Writing (but not reading interestingly enough) will cause a panic. You probably don’t want a nil map.

literalStyle := map[string]int{} using the literal syntax is just fine, though idiomatically it’s probably best to use a make function. Developers are more used to seeing a make function and make offer some additional features.

newStyle := new(map[string]int) creates a pointer to a nil map… very often not what you want.

makeStyle := make(map[string]int) This is probably what you want! If you know your space requirements you can optimize for allocation by passing in a size:

// Give me a map with room for 10 items before needing to allocate more space makeStyle := make(map[string]int, 10)
Code language: Go (go)

Check out our How To: Global Constant Maps and Slices article if you want to learn more about the proper use of maps and slices in Go.

Trying to find your next programming job?

If you are a self-taught developer having trouble finding your first programming job, we've got your back! We have the learning resources and tight-knit dev community that you need to land the coding job you've been looking for. To get started, create a free account and join our Discord community.

Have questions or feedback?

If we've made a mistake in the article, please let us know so we can get it corrected!

Leave a Comment