In this blog, we will be looking at Golang Map Syntax and Creation.
Let’s see what we will be learning about Golang Map Syntax and Creation!
- Golang Maps
- What are maps in Golang? Why maps are needed in Golang?
- Creation of Maps in Golang.
- Find Size of Golang Map.
- Check if Key exists in Golang Map.
- Delete a Key-Value pair in Map.
Before learning Golang Maps and Structs, also learn:
Golang Map Syntax
Maps are type of collection in golang, it contains data in key-pair format.
package main
import (
"fmt"
)
func main() {
csMarks := map[string]int {
"Divyanshu Shekhar" : 100,
"Arpit Sahu" : 99,
"Shubham Ranjan" : 90,
"Sourabh Kumar" : 95,
}
fmt.Println(csMarks)
}
Output:
map[Arpit Sahu:99 Divyanshu Shekhar:100 Shubham Ranjan:90 Sourabh Kumar:95]
Go Map Declaration
Golang Map to String
In this Golang map key is string and value is integer type.
Golang Map Keys
Go Slices cannot be used as a key type of Golang Maps.
Go Slices are of reference types and thus can’t be used as key in maps.
// Slice as key type
m := map[[]string]int{}
fmt.Println(m)
Error :- invalid map key type []string
Learn more about slices in Golang.
Golang Arrays can be used as a key type of Golang Maps.
Arrays are of value types and thus can be used as key of map.
m := map[[3]string]int{}
fmt.Println(m)
Output:
map[]
Learn more about arrays in Golang.
Golang Maps declaration using Make Function
package main
import (
"fmt"
)
func main() {
csMarks := make(map[string]int)
csMarks = map[string]int{
"Divyanshu Shekhar": 100,
"Arpit Sahu": 99,
"Shubham Ranjan": 90,
"Sourabh Kumar": 95,
}
fmt.Println(csMarks)
}
Output:-
map[Arpit Sahu:99 Divyanshu Shekhar:100 Shubham Ranjan:90 Sourabh Kumar:95]
Golang Maps Dereferencing
<map_name>[<key>]
fmt.Println(csMarks["Divyanshu Shekhar"])
Output:
100
Golang Map Delete
delete(<map_name>,<key_name>)
package main
import (
"fmt"
)
func main() {
csMarks := make(map[string]int)
csMarks = map[string]int{
"Divyanshu Shekhar": 100,
"Arpit Sahu": 99,
"Shubham Ranjan": 90,
"Sourabh Kumar": 95,
"xyz": 50,
}
delete(csMarks, "xyz")
fmt.Println(csMarks)
}
Output:
map[Arpit Sahu:99 Divyanshu Shekhar:100 Shubham Ranjan:90 Sourabh Kumar:95]
Check if Key exists in Golang Map
we deleted the “XYZ” key from the csMarks map, what if we try to access the deleted key or any key which is not present in the map.
Let’s see!
package main
import (
"fmt"
)
func main() {
csMarks := make(map[string]int)
csMarks = map[string]int{
"Divyanshu Shekhar": 100,
"Arpit Sahu": 99,
"Shubham Ranjan": 90,
"Sourabh Kumar": 95,
"xyz": 50,
}
delete(csMarks, "xyz")
fmt.Println(csMarks)
fmt.Println(csMarks["xyz"])
fmt.Println(csMarks["abc"])
}
Output:
map[Arpit Sahu:99 Divyanshu Shekhar:100 Shubham Ranjan:90 Sourabh Kumar:95]
0
0
we get the output as 0 for the key “xyz” and “abc”, But why if the keys are not present in the map. This can also cause confusion in your program as you might think does “xyz” has got 0 marks in Computer Science.
To make this correct, there’s a way!
Golang Read only Variable
I am not sure if this is the correct name of the function but we can call it like this.
Let’s see it’s function and how it corrects the previous error.
xyz, ok := csMarks["xyz"]
ds, dsok := csMarks["Divyanshu Shekhar"]
fmt.Println(xyz, ok)
fmt.Println(ds, dsok)
Output:
0 false
100 true
The ok variable tells us whether the key is present in the map or not. It returns true if the value is present in the map, and false if the value is not present in the map
If we just want to check the presence of the key in the map, we can throw the value and just store the comma-ok value.
_, ok := csMarks["xyz"]
_, dsok := csMarks["Divyanshu Shekhar"]
fmt.Println(ok)
fmt.Println(dsok)
Output:
false
true
Golang Map Size using len() function
len(<map_name>)
csMarks := make(map[string]int)
csMarks = map[string]int{
"Divyanshu Shekhar": 100,
"Arpit Sahu": 99,
"Shubham Ranjan": 90,
"Sourabh Kumar": 95,
"xyz": 50,
}
delete(csMarks, "xyz")
fmt.Println("Length of Map: ", len(csMarks))
Output:
Length of Map: 4
Golang Maps Reference Types
package main
import (
"fmt"
)
func main() {
csMarks := make(map[string]int)
csMarks = map[string]int{
"Divyanshu Shekhar": 100,
"Arpit Sahu": 99,
"Shubham Ranjan": 90,
"Sourabh Kumar": 95,
"xyz": 50,
}
marks := csMarks
delete(csMarks, "xyz")
fmt.Println(csMarks)
fmt.Println(marks)
}
Output:
map[Arpit Sahu:99 Divyanshu Shekhar:100 Shubham Ranjan:90 Sourabh Kumar:95]
map[Arpit Sahu:99 Divyanshu Shekhar:100 Shubham Ranjan:90 Sourabh Kumar:95]
we assigned the csMarks map in the marks map, and we deleted key “xyz” from csMarks, but we find out that “xyz” key also gets deleted from the marks map.
This happens because csMarks and marks access the same map and thus when a key is deleted from one map, the effect is also seen on the original map.
So, Maps in Golang are of Reference types.
After learning about maps lets learn about Structs in Golang.
Learn more about Golang map Syntax and Creation from the official documentation.
2 Responses
Good๐๐
Keep going๐๐