75

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user316368
  • 1,135
  • 2
  • 9
  • 12

3 Answers3

180

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

Aditya
  • 1,136
  • 2
  • 12
  • 19
themue
  • 7,626
  • 2
  • 25
  • 28
  • 10
    Take a look here: https://dave.cheney.net/2017/04/29/there-is-no-pass-by-reference-in-go – Joppe Apr 30 '17 at 08:50
  • Yes, Dave described it better. Read his blog entry. – themue May 01 '17 at 15:12
  • It's interesting that on the one hand you can say a map is always a reference. Meaning a map value is a pointer. so when you use the variable of the map, you're utilizing this pointer to set something in the map. And on the other hand a map is not a reference. Because the map value is a pointer, and the pointer value is copied around and the dereferencing happens automatically. – Yehuda Makarov Jun 17 '20 at 16:32
53

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 6
    @JuliusF both, but I am more precise in my answer: the pointer is passed by value. More on the "pass by value" at http://stackoverflow.com/a/23046811/6309. – VonC Apr 15 '14 at 21:18
  • 1
    @VonC thank you so much for this. I will repeat this is my head now ten times every night before going to sleep "the pointer is passed by value". – Xam Eseerts Jul 19 '18 at 07:53
  • @XamEseerts Great! Next step in your pre-nocturnal habits: https://arpitbhayani.me/techie/how-sleepsort-helped-me-understand-concurrency-in-golang.html – VonC Jul 19 '18 at 07:57
8

@Mue 's answer is correct.

Following simple program is enough to validate:

package main

import "fmt"

func main() {
    m := make(map[string]string, 10)
    add(m)
    fmt.Println(m["tom"]) // expect nil ???
}

func add(m map[string]string) {
    m["tom"] = "voldemort"
}

The output of this program is

voldemort

Tf the map was passed by value, then addition to the map in the function add() would not have any effect in the main method. But we see the value added by the method add(). This verifies that the map's pointer is passed to the add() method.

sabbir
  • 686
  • 1
  • 9
  • 15
  • 2
    the map's pointer is passed by value. m["tom"] knows how to dereference whatever pointer `m` actually is. in Go, when you say "the map" it actually means the pointer to the map. Because go syntax abstracted needing to write `*m` away. – Yehuda Makarov Jun 17 '20 at 16:36