In Go, what exactly happens when you pass an argument to a function that takes an interface? Specifically, is is passed by reference or is it passed by value?
Asked
Active
Viewed 60 times
-5
-
3Everything in go is passed by value, including pointers. There is no "pass by reference" in Go. – JimB Aug 31 '20 at 22:30
-
Related / possible duplicate of: [1](https://stackoverflow.com/questions/23542989), [2](https://stackoverflow.com/questions/40577116), [3](https://stackoverflow.com/questions/31932822), [4](https://stackoverflow.com/questions/29155202), [5](https://stackoverflow.com/questions/47296325) – blackgreen Jun 05 '22 at 08:40
1 Answers
1
If you pass an interface value to a function that takes an interface, it is passed without any further processing.
If you pass a value to a function that takes an interface, the compiler creates a copy of that value, then creates an interface containing a pointer to that copy and the type of the value, and passes that.
If you pass a pointer to a function that takes an interface, the compiler creates an interface containing that pointer and the type as a pointer to the value and passes that.
Burak Serdar
- 37,605
- 3
- 27
- 46
-
-
No, it is done at compile time. The compiler knows what is being passed. – Burak Serdar Aug 31 '20 at 22:33