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
Syntax
func make(t Type, size ...IntegerType) Type
Parameters
| Parameter | Description |
|---|---|
[]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
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.
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.
Related
append· Add elements to the end of a slice, growing its backing array when needed; always assign the result back.len and cap· len returns the number of elements in a string, slice, map, array or channel; cap returns a slice's or channel's capacity.- Go Maps · tutorial