File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Languages and the fly likes Go question: make(map) syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Go question: make(map) syntax" Watch "Go question: make(map) syntax" New topic
Author

Go question: make(map) syntax

atul khot
Ranch Hand

Joined: Jul 24, 2008
Posts: 37
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Go question: make(map) syntax
 
Similar Threads
Constructor, Scope, Final variables,
Map : Serious Doubt
A postfix question
weird increment operator behaving more weird when passed to the method
Query in Strings -Pass by Reference