Home
Go
make Keyword (New Slice, Map)
Updated Jan 24, 2025
Dot Net Perls
Make. In the Go language, the slice, map and channel types are created with "make," not "new." This function creates a new instance of a slice, map or channel.
new
The arguments to make indicate what type we are creating, the initial element count, and (optionally) a capacity of the underlying buffer. With a capacity, we can avoid further allocations.
Example. This program uses the make() built-in in several different ways. And it uses an alternative syntax to create empty slices and maps, without calling make.
Part 1 We use make to create a slice of a certain number of elements. With 0, the slice is empty. With 5, the slice has 5 ints of value 0.
Slice
Part 2 With make() calls to create slices, we can use an optional third argument as a hint of what underlying buffer size is desired (a capacity).
Part 3 For slices, it is often better just to use an empty initialization instead of a call to make().
Part 4 Maps can be created too with make. The optional second argument is a capacity hint.
map
Part 5 As with slices, maps often are created without make—an empty initialization can be used.
Part 6 We use make to create a channel with an underlying buffer size of 10 elements. The buffer size is separate from the channel's length.
chan
package main import "fmt" func main() { // Part 1: use make to create an empty slice, and a 5-element slice of all zero values. slice1 := make([]int, 0) slice1 = append(slice1, 10) fmt.Println("make:", slice1) slice2 := make([]int, 5) fmt.Println("make:", slice2) // Part 2: use make to create a zero-element string slice of capacity 20. slice3 := make([]string, 0, 20) slice3 = append(slice3, "cat", "frog", "bird") fmt.Println("make:", slice3) // Part 3: a slice can be created without make. slice4 := []string{} slice4 = append(slice4, "xyz") fmt.Println("[]string:", slice4) // Part 4: use make to create a map with capacity 10. map1 := make(map[string]int, 10) map1["abc"] = 1 fmt.Println("make:", map1) // Part 5: a map can be created without make, but it will not have a custom capacity. map2 := map[string]int{} map2["cat"] = 500 fmt.Println("map[string]int:", map2) // Part 6: use make to create a channel of 10 elements. channel1 := make(chan int, 10) channel1 <- 1 channel1 <- 10 fmt.Println("Channel:", len(channel1)) }
make: [10] make: [0 0 0 0 0] make: [cat frog bird] []string: [xyz] make: map[abc:1] map[string]int: map[cat:500] Channel: 2
Summary. Make is essentially a new() method for slices, maps and channels in the Go language. It is unclear why it is not called "new," but make fills this important initialization need.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 24, 2025 (edit link).
Home
Changes
© 2007-2025 Sam Allen