Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Go question: make(map) syntax

 
Ranch Hand
Posts: 69
  • 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
 
author
Posts: 37
VI Editor Linux 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.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!