Having some maps defined as:
var valueToSomeType = map[uint8]someType{...} var nameToSomeType = map[string]someType{...}
I would want a variable that points to the address of the maps (to don't copy all variable). I tried it using:
valueTo := &valueToSomeType nameTo := &nameToSomeType
but at using valueTo[number]
, it shows
internal compiler error: var without type, init: new
How to get it?
Edit
The error was showed by another problem.
So, to add an element in map we can use one of its member function insert() i.e. pair<iterator,bool> insert (const value_type& element); pair<iterator,bool> insert (const value_type& element); pair<iterator,bool> insert (const value_type& element);
C++ standard provided specialisation of std::less for pointers, so yes you can safely use them as map keys etc.
Maps, like channels, but unlike slices, are just pointers to runtime types. As you saw above, a map is just a pointer to a runtime. hmap structure. Maps have the same pointer semantics as any other pointer value in a Go program.
Maps are reference types, so they are always passed by reference. You don't need a pointer. Go Doc
More specifically, from the Golang Specs:
Slices, maps and channels are reference types that do not require the extra indirection of an allocation with
new
.
The built-in function make takes a typeT
, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions.
It returns a value of typeT
(not*T
).
The memory is initialized as described in the section on initial values
However, regarding function calls, the parameters are passed by value (always).
Except the value of a map parameter is a pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With