Go Map Initialization Methods
In Go language, a map can be initialized and assigned values using two methods:
- Initialize an empty map using the make function, then proceed with assignment operations.
// 初始化一个空的map
m := make(map[string]int)
// 赋值操作
m["key1"] = 1
m["key2"] = 2
- Initialize and assign values to a map literally.
// 初始化并赋值一个map
m := map[string]int{
"key1": 1,
"key2": 2,
}
You can choose either of the two methods to initialize and assign a map.