Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Go question: make(map) syntax

 
atul khot
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Summerfield
author
Ranch Hand
Posts: 37
Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic