SoFunction
Updated on 2025-03-04

Implementation of go language make initialization

One: Overview

In Go, make is a built-in function that initializes slices, maps, and channels. Unlike new functions, make returns the actual object, not a pointer to the object. This article will explore in-depth how make works and provide a series of practical cases to help readers understand and apply make more comprehensively.

Two: Specific explanation

<1> How makes works

The basic syntax of the make function is as follows:

make(type, length, capacity)
  • type: The type of data structure to be created can only be slices, maps, or channels.
  • length: The initial length of the data structure, required for slices and channels.
  • capacity: The capacity of the data structure, optional for slices, required for mappings and channels.

1.1 Initialization of slices

Slices are very flexible data structures in Go language, make is used to create slices and initialize their underlying arrays.

// Create an int type slice with length 3 and capacity 5s := make([]int, 3, 5)
("Slice:", s) // Output: Slice: [0 0 0]("Length:", len(s)) // Output: Length: 3("Capacity:", cap(s)) // Output: Capacity: 5

1.2 Initialization of the map

Maps are key-value pair collections in Go language, make is used to create maps and initialize their underlying hash tables.

// Create an empty string to integer mappingm := make(map[string]int)
("Map:", m) // Output: Map: map[]("Length:", len(m)) // Output: Length: 0

1.3 Initialization of the channel

Channels are synchronous communication mechanisms in Go language, make is used to create channels and initialize their buffers.

// Create an integer channel with capacity 3ch := make(chan int, 3)
("Channel:", ch) // Output: Channel: 0x00("Length:", len(ch)) // Output: Length: 0("Capacity:", cap(ch)) // Output: Capacity: 3

<2>Practical Cases of Make

2.1 Dynamic expansion of slices

Dynamic scaling of slices is an important feature in Go language, making can preset capacity to optimize performance.

s := make([]int, 0, 10) // The preset capacity is 10for i := 0; i &lt; 15; i++ {
    s = append(s, i)
}
("Slice:", s) // Output: Slice: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]

2.2 Mapping concurrency security

The mapping itself is not concurrency-safe, but it can be achieved through the Map type in the sync package.

var m 
("key", 1)
if v, ok := ("key"); ok {
    ("Value:", v) // Output: Value: 1}

2.3 Buffer control of channel

The buffer size of the channel is crucial to controlling the concurrency process.

ch := make(chan int, 2) // Buffer size is 2go func() {
    for i := 0; i &lt; 5; i++ {
        ch &lt;- i * 2 // Send data    }
    close(ch) // Close the channel after sending}()
for v := range ch {
    ("Received:", v) // Receive and print data}

<3> Comparison between make and new

  • new(T) returns a pointer of type T, that is, *T, and make(T) returns an instance of type T.
  • new(T) will not initialize T, while make(T) will initialize according to type.
  • make can only be used for slices, maps, and channels initialization, while new can be used for any type of initialization.

<4>Summary

make is a powerful tool in Go, which provides a simple and efficient way to initialize complex data structures. By using make rationally, the performance and readability of the program can be improved. I hope this article can help readers understand the usage of make more deeply and flexibly use it in actual development.

This is the end of this article about the implementation of go language make initialization. For more related go language make initialization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!