Go 1.27 reference · Built-in Functions

Go make Function

Create and initialise a slice, map or channel, optionally with a length and capacity (slices) or buffer size (channels).

Go 1.27

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

func make(t Type, size ...IntegerType) Type

Parameters

ParameterDescription
[]T, len, cap A slice with length len and capacity cap.
map[K]V, hint A map with room for about hint entries.
chan T, n A channel with buffer size n (0 = unbuffered).

Examples

Example 1
package main

import "fmt"

func main() {
	buf := make([]byte, 0, 1024)
	counts := make(map[string]int, 10)
	jobs := make(chan int, 3)
	counts["go"]++
	jobs <- 1
	fmt.Println(len(buf), cap(buf), counts, len(jobs), cap(jobs))
}

Output

0 1024 map[go:1] 1 3

Run this on your computer — see “Get Started” for setup.

A nil map versus make
package main

import "fmt"

func main() {
	var m map[string]int // nil map: reads work, writes panic
	fmt.Println(m["missing"], len(m))
	m = make(map[string]int)
	m["ok"] = 1
	fmt.Println(m)
}

Output

0 0
map[ok:1]

Run this on your computer — see “Get Started” for setup.

Was this page helpful?