Map Examle | Go Language Examples | Java Inspires



 Hi Guys,

Welcome to Java Inspires ..

Here is the example program of Map in go.


goexample.go

package main

import (
	"fmt"
)

func main() {

	// lets start

	//create an empty map
	var m1 = make(map[string]int)
	//print the map
	fmt.Println("Map :", m1)
	//print length of the map
	fmt.Println("Map length :", len(m1))
	// set eky value pairs into the map
	m1["key1"] = 101
	m1["key2"] = 102
	m1["key3"] = 103

	//print the map
	fmt.Println("Map :", m1)
	//print length of the map
	fmt.Println("Map length :", len(m1))

	//get a value for a key
	v1 := m1["key3"]
	//print the value
	fmt.Println("Value ", v1)

	//get value for unavailable key
	v2 := m1["key5"]
	fmt.Println("v5 :", v2)

	//remove a key/value pair from the map
	delete(m1, "key1")
	//print the map
	fmt.Println("map m1:", m1)

}


Output 

PS D:\MyCode\go\example> go run .\goexample.go
Map : map[]
Map length : 0
Map : map[key1:101 key2:102 key3:103]
Map length : 3
Value  103
v5 : 0
map m1: map[key2:102 key3:103]       
PS D:\MyCode\go\example> 




Post a Comment

Previous Post Next Post