HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

Are maps passed by value or by reference in Go?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arevaluepassedmapsreference

Problem

Are maps passed by value or reference in Go ?

It is always possible to define a function as following, but is this an overkill ?

func foo(dat *map[string]interface{}) {...}


Same question for return value. Should I return a pointer to the map, or return the map as value ?

The intention is of course to avoid unnecessary data copy.

Solution

In this thread you will find your answer :

Golang: Accessing a map using its reference


You don't need to use a pointer with a map.


Map types are reference types, like pointers or slices[1]


If you needed to change the Session you could use a pointer:

map[string]*Session




https://blog.golang.org/go-maps-in-action

Code Snippets

map[string]*Session

Context

Stack Overflow Q#40680981, score: 166

Revisions (0)

No revisions yet.