package main
func main() {
m := make(map[string]int, 3)
x := len(m)
m["Go"] = m["Go"]
y := len(m)
println(x, y)
}
Choices:
Answer: 0 1
Run it on Go play.
Key points:
make
function to create a map, the second argument
is neither the initial length nor the capacity of the result map.
It is just a hint for Go runtime to allocate a large enough backing array
to hold at least the specified number of entries.
m["Go"] = m["Go"]
is equivalent to m["Go"] = 0
.
After the assignment, the map contains one entry.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.