Go Programming Language Map

Go Programming Language Map

In Go programming, map function is used to implement key-value array.

Go Map Example

Explains to define key-value pair array using map function.

Below code defines the map variables, setting values and printing map variables.

package main                                                               	 
import "fmt"                                                               	 
func main() {                                                              	 
	products_code := make(map[string]int)                                  	 
	products_price := make(map[string]float32)                             	 
	fmt.Println("Inserting map data")                                      	 
	// insert map data                                                     	 
	products_code["product1"] = 1                                          	 
	products_price["product1"] = 110.5                                     	 
	products_code["product2"] = 2                                          	 
	products_price["product2"] = 24.75                                     	 
	fmt.Println("Print map data")                                          	 
	// Prints array data                                                   	 
	fmt.Printf("Product (%d) price: %.2f\n", products_code["product1"],products_price["product1"])
	fmt.Printf("Product (%d) price: %.2f\n", products_code["product2"],products_price["product2"])
Output: .
$ go build map.go
$ ./map
Inserting map data
Print map data
Product (1) price: 110.50
Product (2) price: 24.75