Home > Backend Development > Golang > golang make usage

golang make usage

PHPz
Release: 2023-05-21 18:47:38
Original
1361 people have browsed it

make in Golang is a very important built-in function, which is used to create and initialize slices, maps and channels. It also has some usage details to pay attention to, let’s take a closer look.

Basic usage of make

The syntax of make is as follows:

make(t Type, size ...IntegerType) Type
Copy after login

Among them, t represents the type to be created, and size represents the length of the container. Generally speaking, size is only effective for slices, maps and channels.

Slice

We can use the make function to create a slice:

slice := make([]int, 10)
Copy after login

The above code will create an integer slice containing 10 elements. The slice type is taken as the first parameter, and the capacity and length are taken as the second parameter.

Mapping

We can use the make function to create the mapping:

m := make(map[string]int)
Copy after login

The above code will create an empty string to integer mapping.

Channel

We can use the make function to create a channel:

ch := make(chan int)
Copy after login

The above code will create an unbuffered channel of type int.

Other uses of make

In addition to the basic usage, the make function can also accept a variable parameter. This parameter is used to specify the capacity of slices, maps, and channels. This parameter only makes sense for slices or channels. If it is omitted, the container will be a bufferless container.

Slice

We can use the make function to create a slice with sufficient capacity:

slice := make([]int, 10, 20)
Copy after login

The above code will create an integer slice with a length of 10 and a capacity of 20.

Mapping

We can use the make function to create a mapping with sufficient capacity:

m := make(map[string]int, 100)
Copy after login

The above code will create a string to integer mapping with an initial capacity of 100 .

Channel

We can use the make function to create a channel with a buffer. The size of the buffer is determined by the second parameter:

ch := make(chan int, 10)
Copy after login

The above code will create an integer channel with 10 buffers.

Conclusion

In Golang, the make function is one of the most commonly used built-in functions when encountering slices, maps, and channels. It can be used to create a slice, map or channel and initialize its length, capacity or buffer size.

It should be noted that if the second parameter is omitted, the container will have zero length or zero buffer. If the second argument is provided, it will be the initial capacity of the container.

When using the make function, be sure to pay attention to its initialization of the length or size of the container to prevent runtime errors.

The above is the detailed content of golang make usage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template