Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing pointers to Maps in Golang

Tags:

pointers

go

map

I am having a little trouble creating pointers to maps in Go. Can you please let me know if I am passing the map parameter correctly? It pairs integer values with structs.

    type symbol_table struct{
                   ---
                   ---
                   ---
    }
    //is the map parameter being called correctly?
func TD(..., symbolMAP *map[int]symbol_table, ...){

                   ---
                   ---
                   ---
    }
    func main(){
               symbolMAP:=make(map[int] symbol_table)
               TD(&symbolMAP)
       }
like image 414
progfan Avatar asked Feb 18 '13 02:02

progfan


People also ask

Is Golang map a pointer?

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.

How do you pass a pointer to a function in Golang?

Go programming language allows you to pass a pointer to a function. To do so, simply declare the function parameter as a pointer type.

Is map passed by value in Golang?

No. Maps are reference by default.


1 Answers

As already noted in the comments, there is no need to pass pointer to a map.

A map is already a reference type. Changes in the map will be observed from other variables.

See also Q/A: Go - Pointer to map.

like image 144
Aleš Avatar answered Oct 14 '22 00:10

Aleš