| Author |
Go question: make(map) syntax
|
atul khot
Greenhorn
Joined: Jul 24, 2008
Posts: 23
|
|
Dear Mark,
Thanks for your answers...
One other thing I felt a little odd was "making of a map"
var m map[string]int = make(map[string]int)
Here, as I understand, the key is a string - and value is an int...
So, does it mean that whatever is in the square brackets ([]) is the type of key - and what immediately follows the closing
] is the type of value...
Is this the correct way to go?
Also,
I solved the gotour exercise no 44 (Exercise: Maps) -
For some reason, I think Go supports post increment - but not per-increment
--- warm regards
atul
|
--cheerio atul
|
 |
Mark Summerfield
author
Ranch Hand
Joined: Jun 20, 2012
Posts: 37
|
|
Hi Atul,
var m map[string]int = make(map[string]int)
Here, as I understand, the key is a string - and value is an int...
Yes, you are quite right. You can also do it like this:
or like this if you want it to start out nonempty:
And, of course, you can use other types too.
You are also right that Go only supports post increment, i++. Also, increment is not an expression, so you can not write j = i++ in Go. This avoids lots of potential problems, e.g., f(i++) and f(++i) are legal in C but not in Go.
And in your solution to exercise 44 you return a map that you've allocated inside a function: and this is perfectly good Go style (but would be a disaster in C/C++!) thanks to Go's memory management.
|
Mark Summerfield
"Programming in Go" - http://www.qtrac.eu/gobook.html
|
 |
 |
|
|
subject: Go question: make(map) syntax
|
|
|