Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar

cjgiridhar/declare_maps.go

Last active Feb 17, 2019
Embed
What would you like to do?
Declare maps in Golang
package main
import (
"fmt"
)
func main() {
var mscores = make(map[string]int)
mscores["Chetan"] = 90
// Returns mscrores: map[Chetan:90]
fmt.Println("mscrores:", mscores)
var score map[string]int
// Runtime error: panic: assignment to entry in nil map
score["Chetan"] = 90
var students = make(map[string]int)
students["Chetan"] = 90
students["John"] = 75
students["Alice"] = 30
// Returns mscrores: map[John:75 Alice:30 Chetan:90]
fmt.Println("students scrores:", students)
values := map[string]int{
"abc": 123,
"def": 345,
"ghi": 567,
"jkl": 897,
}
// Returns values: map[abc:123 def:345 ghi:567 jkl:897]
fmt.Println("values:", values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.