'Does go copy return value every time? [duplicate]
For example,
func foo() {
return map[string]interface{}{}
}
func baz(){
a := foo()
}
every time the baz() was called, was the map also copied? If yes and the map is very complex, would the process take much time to do the copy operation?
Solution 1:[1]
In that case a new pointer to a map is created each time the function is executed.
Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn’t point to an initialized map.
Also, go has pointers to decide when to return a copy or not. For example https://go.dev/play/p/Pibhj5onIIL
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
