Make, New and Literals Cheat Sheet – Slice and Map Initialization in Go
Tags: cleancode, go, programming
Published: 2020-06-29 13:38:21

---
title: Make, New and Literals Cheat Sheet – Slice and Map Initialization in Go
published: true
date: 2020-06-29 13:31:40 UTC
tags: CleanCode,Golang,Programming,go
canonical_url: https://qvault.io/2020/06/29/make-new-and-literals-cheat-sheet-slice-and-map-initialization-in-go/
---

 ![golang gopher slice literal](https://qvault.io/wp-content/uploads/2020/06/Go_Lang_Gopher-300x169.jpg)

The post [Make, New and Literals Cheat Sheet – Slice and Map Initialization in Go](https://qvault.io/2020/06/29/make-new-and-literals-cheat-sheet-slice-and-map-initialization-in-go/) appeared first on [Qvault](https://qvault.io).

There are quite a few ways to create new maps and slices in Go. Which one is best? Or perhaps better asked, which one is best in your situation? Let’s take a look.

## Slices

```go
var varStyle []string

literalStyle := []string{}

newStyle := new([]string)

makeStyle := make([]string, 0)
```

**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

```go
var varStyle map[int]int

literalStyle = map[string]int{}

newStyle := new(map[string]int)

makeStyle := make(map[string]int)
```

**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 its probably best to use a make function. Developers are more used to seeing a make function and make offers 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:

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

## Thanks For Reading

Hit me up on twitter [@wagslane](https://twitter.com/wagslane) if you have any questions or comments.

Take your coding career to the next level with courses on [Qvault Classroom](https://classroom.qvault.io/#/)

Follow me on Dev.to: [wagslane](https://dev.to/wagslane)

The post [Make, New and Literals Cheat Sheet – Slice and Map Initialization in Go](https://qvault.io/2020/06/29/make-new-and-literals-cheat-sheet-slice-and-map-initialization-in-go/) appeared first on [Qvault](https://qvault.io).