Using Simple example we will learn mapping in Go lang :
- Go program to illustrate how to create maps
package main import "fmt" func main() { var map_1 map[int]int // Creating and initializing empty map if map_1 == nil // Checking if the map is nil or not fmt.Println("True") } else { fmt.Println("False") } // Creating and initializing a map map_2: = map[int]string { 90: "Math", 91: "Statistic", 92: "Python", 93: "SQL", 94: "Machine Learning ", } fmt.Println("Map-2: ", map_2) }
Output :
PS C:\GO_Language\Maps> go run maps.go True Map-2: map[90:Math 91:Statistic 92:Python 93:SQL 94:Tablue]
- Go program to illustrate, iterate a map using the range for loop. The value of this loop may vary because the map is an unordered collection
package main import "fmt" func main() { M_a_p: = map[int]string { 90: "Math", 91: "Statistic", 92: "Python", 93: "SQL", 94: " Machine Learning ", } for id, pet := range m_a_p { fmt.Println(id, pet) } }
Output :
PS C:\GO_Language\Maps> go run maps1.go 92 Python 93 SQL 94 Machine Learning 90 Math 91 Statistic
Using the Mark Function:
- Go program to illustrate the difference between declaring an empty map using with the make()function and without it
package main import "fmt" func main() { var My_course = make(map[float64]string) fmt.Println(My_map) My_course[1.3] = "Data Scientist" My_course[1.5] = "Data Analyst" fmt.Println(My_course) }
Output :
PS C:\GO_Language\Maps> go run maps2.go map[] map[1.3:Data Scientist 1.5:Data Analyst]
- Go program to illustrate the difference between declaring an empty map using with the make()function and without it.
package main import "fmt" // Main function func main() { // Creating and initializing a map m_a_p := map[int]string{ 90: "Math", 91: "Statistic", 92: "Python", 93: "SQL", 94: "Tablue", } // Iterating map using for rang loop for id, pet := range m_a_p { fmt.Println(id, pet) } }
Output :
PS C:\GO_Language\Maps> go run maps3.go 90 Math 91 Statistic 92 Python 93 SQL 94 Tablue
- Go program to illustrate the difference between declaring an empty map using with the make()function and without it
The post Maps in Golang appeared first on Prwatech.
This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here