Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Last active February 17, 2019 01:51
Show Gist options
  • Save cjgiridhar/1c03708c029c1a9ce007cf6883d76c62 to your computer and use it in GitHub Desktop.
Save cjgiridhar/1c03708c029c1a9ce007cf6883d76c62 to your computer and use it in GitHub Desktop.
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