SoFunction
Updated on 2025-03-02

Use of built-in function make in Go language

In Go, make is a very powerful built-in function for creating and initializing specific types of data structures, including slices, maps, and channels. Mastering the use of make can help us manage memory and data structures more efficiently. This article will introduce in detail the usage scenarios and examples of the make function.

The basic syntax of make function

The basic syntax of the make function is as follows:

make(type, size, [capacity])
  • type: Specifies the type of data structure to create, which can be slices, maps, or channels.
  • size: Specifies the initial length (for slices) or the initial size (for channels).
  • capacity (optional): Applicable to slices only, specifying its capacity.

Create slices

When creating slices using the make function, you can specify the initial length and capacity of the slice. If no capacity is specified, the default is the same as the length.

package main

import "fmt"

func main() {
    // Create slices of length 5    slice1 := make([]int, 5)
    ("slice1:", slice1) // Output: slice1: [0 0 0 0 0 0]
    // Create slices with length 5 and capacity 10    slice2 := make([]int, 5, 10)
    ("slice2:", slice2) // Output: slice2: [0 0 0 0 0 0]    ("slice2 capacity:", cap(slice2)) // Output: slice2 Capacity: 10}

Create a map

When creating a map with the make function, you only need to specify the type of the map. The map automatically resizes, so there is no need to specify an initial capacity, but an initial capacity can be provided for improved performance.

package main

import "fmt"

func main() {
    // Create a map    m := make(map[string]int)
    m["a"] = 1
    m["b"] = 2
    ("map:", m) // Output: map: map[a:1 b:2]
    // Create a map with an initial capacity of 10    m2 := make(map[string]int, 10)
    ("map2:", m2) // Output: map2: map[]}

Create a channel

When creating a channel using the make function, you need to specify the type and buffer size of the channel. If the buffer size is 0, an unbuffered channel is created.

package main

import "fmt"

func main() {
    // Create an unbuffered channel    ch1 := make(chan int)
    go func() {
        ch1 <- 1
    }()
    ("ch1:", <-ch1) // Output: ch1: 1
    // Create a channel with buffer size 5    ch2 := make(chan int, 5)
    ch2 <- 1
    ch2 <- 2
    ("ch2 Capacity:", cap(ch2)) // Output: ch2 Capacity: 5    ("ch2 length:", len(ch2)) // Output: ch2 Length: 2}

Notes on using make function

  • Different from new: the make function is only used to create slices, maps, and channels, and returns an initialized data structure; the new function is used to allocate memory and returns a pointer to a zero value.
  • Initial Capacity: Specifying initial capacity for slices or maps can improve performance, especially if large amounts of data are expected to be stored.
  • Unbuffered and buffered channels: Unbuffered channels are used for synchronization, while channels with buffers allow asynchronous operations. Choose the appropriate channel type according to your needs.

Sample code

Here is a comprehensive example showing how to create slices, maps, and channels using the make function:

package main

import "fmt"

func main() {
    // Create slices    slice := make([]int, 5, 10)
    ("slice:", slice) // Output: Slice: [0 0 0 0 0]    ("Slice Capacity:", cap(slice)) // Output: Slice capacity: 10
    // Create a map    m := make(map[string]int)
    m["a"] = 1
    m["b"] = 2
    ("Mapping:", m) // Output: Map: map[a:1 b:2]
    // Create a channel    ch := make(chan int, 5)
    ch <- 1
    ch <- 2
    ("Channel Capacity:", cap(ch)) // Output: Channel capacity: 5    ("Channel Length:", len(ch)) // Output: Channel length: 2}

Summarize

make is a key function in the Go language that helps us create and initialize slices, maps, and channels efficiently. Understanding and correct use of make functions can significantly improve the performance and maintainability of your code. I hope this blog can help you better understand and use the make functions in Go language, making your programming journey smoother.

This is the end of this article about the use of built-in function make in Go language. For more related content on built-in function make, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!