• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Go question: make(map) syntax

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 37
VI Editor Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic