PHP turn Go Series: map map

Mapping definition

Acquaintance map will be very ignorant, because there is no type of mapping is defined in PHP. In fact, not so complicated, any complex types can be used in a PHP array representation, the mapping is no exception.

$array['name'] = '平也';
$array['sex'] = '1';
$array['age'] = '10';

//output
Array
(
    [name] => 平也
    [sex] => 1
    [age] => 10
)

In fact, there are key mapping is an array of value assignment in Go is also very similar, but need to declare in advance the type of keys and values ​​of the mapping type, the type of assignment to ensure the unity of all keys and values, otherwise it will error.

array := make(map[string]string)
array["name"] = "平也"
array["sex"] = "1"
array["age"] = "10"
fmt.Print(array) //output map[age:10 name:平也 sex:1]

There is a method of initializing the array, that is, all the keys and values ​​assigned to variables to be stored in PHP.

$array = [
    'name' => '平也',
    'sex' => '1',
    'age' => '10'
];

A similar method of initialization in Go, the data type of the value unity and remember keys.

array := map[string]string{
    "name": "平也",
    "sex":  "1",
    "age":  "10",
}

Map traversal

In PHP is actually traversal operation array, foreach can be.

$array = [
    'name' => '平也',
    'sex' => '1',
    'age' => '10'
];

foreach ($array as $key => $value) {
    print_r($array);
}

//output
Array
(
    [name] => 平也
    [sex] => 1
    [age] => 10
)
Array
(
    [name] => 平也
    [sex] => 1
    [age] => 10
)
Array
(
    [name] => 平也
    [sex] => 1
    [age] => 10
)

In Go you can also traverse the map like that traverse the array, still using the range keyword.

array := map[string]string{
    "name": "平也",
    "sex":  "1",
    "age":  "10",
}
for v, k := range array {
    fmt.Print(k, v)
}

The articles can be ignored by the underscore key or value mentioned traversal, if only traversal key, underline can be omitted.

array := map[string]string{
    "name": "平也",
    "sex":  "1",
    "age":  "10",
}
for k := range array {
    fmt.Print(k)
}
//output sexagename

Mapping values

PHP values ​​can be directly read by key groups.

$array = ['name' => 'pingye'];
echo $array['name']; //output pingye

Operation in Go is the same, unlike PHP is taken if the key does not exist, the default output Go null value, a warning is generated in PHP warning.

array := map[string]string{
    "name": "pingye",
    "sex":  "1",
    "age":  "10",
}
fmt.Print(array["name"]) //pingye

Mapping elements deleted

unset in PHP can delete any array elements you want to delete, and very easy to use.

$array = [
    'name' => '平也',
    'sex' => '1',
    'age' => '10'
];
unset($array['name']);
print_r($array);

//output
Array
(
    [sex] => 1
    [age] => 10
)

Delete elements in Go to the map through the delete function.

array := map[string]string{
    "name": "pingye",
    "sex":  "1",
    "age":  "10",
}
delete(array, "name")
fmt.Print(array) //output map[age:10 sex:1]

Empty map elements

Never seem to pay attention in PHP over whether to clear the array, I'm sorry, empty array method I can think of it is to empty array assigned to it.

$array = [
    'name' => '平也',
    'sex' => '1',
    'age' => '10'
];
$array = [];
print_r($array);
//output
Array
(
)

However, in Go does not provide a function to clear the map, to re-make a map on the line, garbage collection is the original map Go to clear out, write a function efficiency than even empty still high. These are different from PHP and Go Contact map in the map area, if interested can learn on their own try.

Guess you like

Origin www.cnblogs.com/enochzzg/p/11022940.html