Skip to content
Home » How to create a simple map in Golang

How to create a simple map in Golang

Learn about How to create a simple map in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to create a simple map in Golang

// Golang program to create a simple map

package main

import "fmt"

func main() {
	CountryCode := make(map[string]int)

	CountryCode["ind"] = 101
	CountryCode["aus"] = 102
	CountryCode["eng"] = 103
	CountryCode["pak"] = 104
	CountryCode["usa"] = 105

	fmt.Println("India    :", CountryCode["ind"])
	fmt.Println("Australia:", CountryCode["aus"])
	fmt.Println("England  :", CountryCode["eng"])
	fmt.Println("Pakistan :", CountryCode["pak"])
	fmt.Println("USA      :", CountryCode["usa"])
}

In the above program, we declare the package main. The main package instructs the Go language compiler that the package must be compiled and an executable file generated. We imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. In the main() function, we created a CountryCode map using make() function to store country code of specified country. Map store items in KEY/VALUE pair.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to demonstrate the fmt.Sprintf() function in Golang
How to demonstrate the fmt.Sscanf() function in Golang