Declare Map in Golang

Create new folder named src. In src folder, create new file named main.go as below:

package main

import "fmt"

func main() {
	Demo1()
	Demo2()
	Demo3()
	Demo4()
}

func Demo1() {
	student := make(map[string]string)
	student["id"] = "st01"
	student["name"] = "name 1"
	student["address"] = "address 1"

	fmt.Println("Student Info")
	fmt.Println(student)

	fmt.Println("Student Info")
	for key, value := range student {
		fmt.Println(key, ":", value)
	}
}

func Demo2() {
	student := map[string]string{
		"id":      "st01",
		"name":    "name 1",
		"address": "address 1",
	}
	student["phone"] = "12345"

	fmt.Println("Student Info")
	fmt.Println(student)

	fmt.Println("Student Info")
	for key, value := range student {
		fmt.Println(key, ":", value)
	}
}

func Demo3() {
	student := map[string]string{
		"id":      "st01",
		"name":    "name 1",
		"address": "address 1",
	}
	value, ok := student["name"]
	if ok {
		fmt.Println(value)
	} else {
		fmt.Println("error")
	}
}

func Demo4() {
	student := make(map[string]interface{})
	student["id"] = "st01"
	student["name"] = "name 1"
	student["address"] = "address 1"
	student["age"] = 20
	student["score"] = 7.8
	student["status"] = true

	fmt.Println("Student Info")
	fmt.Println(student)

	fmt.Println("Student Info")
	for key, value := range student {
		fmt.Println(key, ":", value)
	}
}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Student Info
map[id:st01 name:name 1 address:address 1]

Student Info
id : st01
name : name 1
address : address 1

Student Info
map[id:st01 name:name 1 address:address 1 phone:12345]

Student Info
id : st01
name : name 1
address : address 1
phone : 12345

name 1

Student Info
map[name:name 1 address:address 1 age:20 score:7.8 status:true id:st01]

Student Info
name : name 1
address : address 1
age : 20
score : 7.8
status : true
id : st01