Home >Backend Development >Golang >Is map in golang a pointer?

Is map in golang a pointer?

尚
Original
2019-12-24 11:39:366710browse

Is map in golang a pointer?

map in golang is not a pointer.

Pointer:

A pointer variable points to the memory address of a value.

Similar to variables and constants, you need to declare a pointer before using it. The pointer declaration format is as follows:

var var_name *var-type

var-type is the pointer type, var_name is the pointer variable name, and the * sign is used to specify that the variable is used as a pointer. The following are valid pointer declarations:

map:

Map is an unordered collection of key-value pairs. The most important point of Map is to quickly retrieve data through key. Key is similar to an index and points to the value of the data.

Map is a collection, so we can iterate over it just like arrays and slices. However, Map is unordered and we cannot determine the order in which it is returned. This is because Map is implemented using a hash table.

Define Map

You can use the built-in function make or the map keyword to define Map:

/* 声明变量,默认 map 是 nil */
var map_variable map[key_data_type]value_data_type
/* 使用 make 函数 */
map_variable := make(map[key_data_type]value_data_type)

If the map is not initialized, a nil map will be created. nil map cannot be used to store key-value pairs

For more golang knowledge, please pay attention to the PHP Chinese websitegolang tutorial column.

The above is the detailed content of Is map in golang a pointer?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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