Introduction to Golang Map
In Golang Map, the map allows us to store data in an unordered manner, in map data can be stored in the key-value pair pattern, we use the map data because it allows us faster and fetching and updating and deleting of the attribute by using the key of any value, we can say it is similar to any hash data type, storing data in the map is less expensive because it takes the smallest size of storage for data, for example, if your machine is of 64 bit, in that case, it takes 8 bytes and for a machine of 32 bit, it will take 4 bytes of space.
Syntax
Below is a simple syntax for the map in go language, we can explain the attribute of the below syntax in the following steps.
map[type_of_key]type_of_value{}
map[type_of_key]type_of_value{id1: data1, ..., idN: dataN}
Parameters:
- map: the map is a data type that takes two arguments, these two arguments are basically about the type of data that we are going to store.
- type_of_key: Here in the type section we need to mention the data types of the key as we said we will pass arguments to the map, so this is the one argument that tells the compiler about the type of key or key-value pair of data which we are going to store.
- type_of_value: This is the second attribute of the map which tells about the data type of the value which we are going to store.
How does a Map work in Go language?
As we discussed that map is another data type that allows us to store unordered data types like hash with key-value pairs, let us discuss the working of it. It takes very little memory for storage or simply we can say it is optimized (4byte for 32-bit machine and 8 bytes for the 64-bit machine). It is similar to the hash, like key-value pairs.
A map key will always have a unique value. Map values may be or may not be unique. Data types of the keys and data types of the values must be the same. This means the data type of all the keys must be similar in a map and similarly, the datatype of value inside a map must be similar. Here map allows us to take different types of keys and values. This means data type of key and value can be different. We can access the attributes of the map with the key and this makes it very useful as we can easily update, delete and retrieve with key .like map_name[key], here key can be key=10,100, etc.
Examples to Implement Golang Map
To run the below code example we can create a file with name map.go and paste the code example on the file and run the command go run map.go and we can get the output.
Example #1
Below is a simple example where we are creating a map data with some students list, we have taken some names and some keys for each name. We can see we have taken unique keys for each. We can take duplicate values for the keys . Please see the below example along with the screen of output.
Code:
package main
import "fmt"
func main() {
var students_map map[int]int
if students_map == nil {
fmt.Println("Student list is empty")
} else {
fmt.Println("Student List is not empty")
}
students_map2 := map[int]string{
1: "Ranjan",
2: "Ajay",
3: "Vijay",
4: "Sujit",
5: "Akash",
}
fmt.Println("Students", students_map2)
}
Output:
Example #2
In the below example, we are creating user map data with help of the function make and after that, we are assigning the different values on keys. Please see the below example along with the screen of output:
Code:
package main
import "fmt"
func main() {
var users = make(map[float64]string)
fmt.Println(users)
users[8] = "Ranjan"
users[13] = "Vikash"
users[81] = "Akash"
users[131] = "Suraj"
fmt.Println(users)
}
Output:
Example #3
In this example we are looking at the iteration on the map data, we have to use the for loop here with the range . Here in the example below, we are getting two things roll no and name, we can write anything in place of these attributes. Attribute at first place is key and the attribute at second place is value. Please see the below example along with the screen of output:
Code:
package main
import "fmt"
func main() {
students := map[int]string{
111: "Ranjan",
112: "Ajay",
113: "Vijay",
114: "Sujit",
115: "Akash",
}
for rollno, name := range students {
fmt.Println(rollno, name)
}
}
Output:
Example #4
In the below example we are trying to access the value with the direction key. We have passed the key to the map data and if it matches an existing key then it will return the value stored for that key. Please see the below example along with the screen of output:
Code:
package main
import "fmt"
func main() {
students := map[int]string{
111: "Ranjan",
112: "Ajay",
113: "Vijay",
114: "Sujit",
115: "Akash",
}
fmt.Println(students)
student_1 := students[111]
student_2 := students[113]
fmt.Println("The roll number [111]: ", student_1)
fmt.Println("Value of roll number [113]: ", student_2)
}
Output:
Example #5
In the below example we are checking if the key is available inside the map or not. Please see the below example along with the screen of output:
Code:
package main
import "fmt"
func main() {
students := map[int]string{
111: "Ranjan",
112: "Ajay",
113: "Vijay",
114: "Sujit",
115: "Akash",
}
student_name1, check1 := students[111]
fmt.Println("\n The student availability is:", check1)
fmt.Println("The Student name is:", student_name1)
_, check2 := students[114]
fmt.Println("\n The student availability is:", check2)
_, check3 := students[114]
fmt.Println("\n The student availability is:", check3)
}
Output:
Conclusion
From this tutorial we learned the basic concept of the go map data, we learned the way of storing data and its working. We learned concepts like storing, accessing, and attributes of the map with the help of simple syntax and some of the important examples.
Recommended Articles
This is a guide to Golang Map. Here we discuss an introduction, syntax, and working of Golang Map along with examples and code implementation. You can also go through our other related articles to learn more –