Download our all in one automation tool for various social media websites.
The syntax to declare a map is:
var mapIdentifier map[typeOfKey]typeOfValue
Where typeOfKey
is the type of key that will be used while defining a map key
and typeOfValue
will be used for defining the type of values that will be
stored in the map.
Program given below demonstrates various ways by which an empty map can be defined.
package main
import "fmt"
func main() {
// using var for empty map declaration
var emptyMap1 map[string]int
// defining an empty map using make
emptyMap2 := make(map[int]int)
fmt.Println(emptyMap1)
fmt.Println(emptyMap2)
}
Above program produces following output:
map[]
map[]
Program given below demonstrates how to access and change values that already exist in a map.
package main
import "fmt"
func main() {
var exampleMap = map[int]string{
1: "one",
2: "two",
3: "three",
}
// accessing value
fmt.Println("exampleMap[1] :",exampleMap[1])
// change value
exampleMap[1]="four"
// print new value to console
fmt.Println("exampleMap[1] :",exampleMap[1])
}
Above program produces following output:
exampleMap[1] : one
exampleMap[1] : four
Program given below demonstrates how to declare and initialize a map at the same time using the map using map literals.
While initializing a map using map literals, keys are mandatory.
package main
import "fmt"
func main() {
var map1 = map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
fmt.Println(map1)
}
Above program produces following output:
map[a:1 b:2 c:3 d:4]
fmt.Println
prints map objects in the form of
map[key1:value1 key2:value2]
.
In Golang map keys can not be duplicate, however map values can be duplicate as demonstrated in the example given below:
var emptyMap1 = map[string]int{
"a": 1,
// duplicate map value is legal
"b": 1,
// duplicate map key is not legal
"a": 1,
}
In Golang, we can iterate over maps using the range form of the for loop.
Using for range loop we can access individual key value pairs of the a map as shown in the example given below:
// iterating a map in Golang
package main
import "fmt"
func main() {
var exampleMap = map[string]string{
"key_1": "value_1",
"key_2": "value_2",
"key_3": "value_3",
}
for key, value := range exampleMap {
fmt.Println("key is:", key)
fmt.Println("value is:", value)
}
}
Above program produces following output:
key is: key_2
value is: value_2
key is: key_3
value is: value_3
key is: key_1
value is: value_1
To only access the key, the loop can be modified as follows:
for key := range exampleMap {
fmt.Println("key is:", key)
}
To only access the value of elements in the map, then the loop can be modified as follows:
for _,value := range exampleMap {
fmt.Println("value is:", value)
}
Program given below demonstrates how to calculate length of a map using the
built in len
function. len
function can also be used to calculate length of
other data types such as arrays, slices and more.
package main
import "fmt"
func main() {
var exampleMap = map[int]string{
1: "one",
2: "two",
3: "three",
}
fmt.Println("length of the map is:", len(exampleMap))
}
Above program produces following output:
length of the map is: 3
Go provides a built in function called delete
for deleting elements inside a
map. Using this function, values stored inside map can be deleted by providing
its key.
Program given below demonstrates how to use delete
function to delete a
specific element inside a map using its key.
First argument passed to delete
function is the map that is being altered,
second argument is the key that will be getting deleted from the map.
If the key that is being passed to the delete
function does not exist then
Golang won’t throw an error instead program execution will continue as expected.
package main
import "fmt"
func main() {
var exampleMap = map[int]string{
1: "one",
2: "two",
3: "three",
}
// delete map value with key 1
delete(exampleMap,1)
// no error even if key `100` does not exist
delete(exampleMap,100)
// print exampleMap to console
fmt.Println(exampleMap)
}
Above program produces following output:
map[2:two 3:three]
When an existing map is assigned to a newly declared map then newly declared map is only a reference to the original map. If any changes are made to the newly referenced map then these changes are also reflected on the original map as well.
To create a true new copy of the map individual elements of the map must be manually copied instead of referencing the original map.