Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to a map

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.

like image 258
user316368 Avatar asked May 11 '10 09:05

user316368


People also ask

How do you add a pointer to a map?

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);

Can you use a pointer as a key to a map?

C++ standard provided specialisation of std::less for pointers, so yes you can safely use them as map keys etc.

Is a map a pointer Golang?

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.


2 Answers

Maps are reference types, so they are always passed by reference. You don't need a pointer. Go Doc

like image 92
themue Avatar answered Sep 18 '22 13:09

themue


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 type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions.
It returns a value of type T (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.

like image 20
VonC Avatar answered Sep 17 '22 13:09

VonC