post image January 6, 2022 | 1 min Read

Go maps

func maps()  {
	
	statePopulation := make(map[string]int)
	statePopulation = map[string]int{
		"California": 2341232,
		"Texas": 3341232,
		"Florida": 4341232,
		"New York": 5341232,
		"Illinois": 6341232,
		"Ohio": 7341232,
	}

	fmt.Printf("State population: %v\n\n", statePopulation)

	fmt.Printf("State California: %v\n", statePopulation["California"])
	fmt.Printf("State Texas: %v\n", statePopulation["Texas"])
	fmt.Printf("State Florida: %v\n", statePopulation["Florida"])
	fmt.Printf("State New York: %v\n", statePopulation["New York"])
	fmt.Printf("State Illinois: %v\n", statePopulation["Illinois"])
	fmt.Printf("State Ohio: %v\n", statePopulation["Ohio"])
	
	statePopulation["Georgia"] = 11111

	fmt.Printf("Length of map: %v\n", len(statePopulation))

	fmt.Printf("State Ohio: %v\n", statePopulation["Ohio"])

	delete(statePopulation, "Ohio")

	fmt.Printf("State population: %v\n\n", statePopulation)

	// how about if we ask about non existing key ???

	newStatePopulation, ok := statePopulation["xyz"]
	fmt.Printf("The \"xyz\" key is present in map: %v (%v)\n", newStatePopulation, ok)

	_, okk := statePopulation["xtt"]
	fmt.Printf("The \"xyz\" key is present in map: (%v)\n", okk)

}

func main()  {
	// arrays()
	// slices()
	maps()
}
author image

Jan Toth

I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …

comments powered by Disqus