Definition of mapping
Getting to know a mapping can be confusing because there is no definition of mapping types in PHP. It’s not that complicated. Any complex type can be represented by arrays in PHP, and maps are no exception.
$array['name'] = 'flat also';
$array['sex'] = '1';
$array['age'] = '10';
//output
Array[sex] => [sex] =>1
[age] => 10
)
Copy the code
A mapping is actually an array with keys and values, and the assignment value in Go is similar, but the key and value types of the mapping type need to be declared in advance to ensure that the assignment types of all keys and values are the same, otherwise an error will be reported.
array := make(map[string]string)
array["name"] = "Flat"
array["sex"] = "1"
array["age"] = "10"
fmt.Print(array) //output map[age:10 name: 9999sex :1]
Copy the code
Another way to initialize an array in PHP is to assign all the stored keys and values to variables.
$array = [
'name'= >'flat also'.'sex'= >'1'.'age'= >'10'
];
Copy the code
There is a similar initialization method in Go, but keep in mind that the data type is consistent with the key and value.
array := map[string]string{
"name": "Flat"."sex": "1"."age": "10",}Copy the code
Traversal of the map
In PHP, you’re just going to iterate over a group of numbers. Foreach works.
$array = [
'name'= >'flat also'.'sex'= >'1'.'age'= >'10'
];
foreach ($array as $key => $value) {
print_r($array);
}
//output
Array[sex] => [sex] =>1
[age] => 10
)
Array[sex] => [sex] =>1
[age] => 10
)
Array[sex] => [sex] =>1
[age] => 10
)
Copy the code
A map can also be traversed in Go like a group of numbers, still using the range keyword.
array := map[string]string{
"name": "Flat"."sex": "1"."age": "10",}for v, k := range array {
fmt.Print(k, v)
}
Copy the code
In the previous article, you could omit keys or values by underlining them, or omit them if you only iterate over keys.
array := map[string]string{
"name": "Flat"."sex": "1"."age": "10",}for k := range array {
fmt.Print(k)
}
//output sexagename
Copy the code
Mapping value
In PHP, the value can be evaluated directly by the key of the reading group.
$array = ['name'= >'pingye'];
echo $array['name']; //output pingye
Copy the code
The operation in Go is the same. Unlike PHP, if a nonexistent key is taken, Go defaults to a null value, which in PHP generates a warning.
array := map[string]string{
"name": "pingye"."sex": "1"."age": "10",
}
fmt.Print(array["name"]) //pingye
Copy the code
Deletion of mapping elements
Unset in PHP can delete any array element you want, which is very useful.
$array = [
'name'= >'flat also'.'sex'= >'1'.'age'= >'10'
];
unset($array['name']);
print_r($array);
//output
Array
(
[sex] => 1
[age] => 10
)
Copy the code
The map elements are deleted by the delete function in Go.
array := map[string]string{
"name": "pingye"."sex": "1"."age": "10",}delete(array, "name")
fmt.Print(array) //output map[age:10 sex:1]
Copy the code
Clearing map elements
I don’t think I’ve ever noticed in PHP whether to empty an array or not, sorry, but the only way I can think of to empty an array is to assign an empty array to it.
$array = [
'name'= >'flat also'.'sex'= >'1'.'age'= >'10'
];
$array = [];
print_r($array);
//output
Array
(
)
Copy the code
However, there is no function to clear a map in Go. Just make a new map and the original map will be cleared by Go’s garbage collection mechanism, which is even more efficient than writing a clear function. The above is the difference between PHP and Go in map mapping. If you are interested, you can try it by yourself.