Last active
February 17, 2019 01:51
-
-
Save cjgiridhar/1c03708c029c1a9ce007cf6883d76c62 to your computer and use it in GitHub Desktop.
Declare maps in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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