The examples have gotten pretty complex, but that has been necessary in order to ask this question: What is being assigned to y
in the following example?
var x int64
var y interface{}
x = 2048
y = x
Because of how interfaces "wrap" underlying types and values, we might be tempted to say The variable y
is assigned x
. Except that is not entirely accurate. Let's use a different example:
var x int64
var y int64
x = 2048
y = x
Same question. I think we would all answer The variable y
is assigned the value of the variable x
. That would be correct. And we know that modifying x
later would not affect y
(Golang playground):
package main
import "fmt"
func main() {
var x int64
var y int64
x = 2048
y = x
fmt.Printf("x=%d, y=%d\n", x, y)
x = 4096
fmt.Printf("x=%d, y=%d\n", x, y)
}
The above example of course emits:
x=2048, y=2048
x=4096, y=2048
So why would think anything different should happen when y
is an interface (Golang playground)?
var y interface{}
The program emits the same output as before:
x=2048, y=2048
x=4096, y=2048
This is because storing a value in an interface results in a copy of that value, just like when assigning a value to another variable of the same type. Even if the value is a pointer, you are creating a copy of that pointer.
This does raise the question though -- if an interface is just two addresses to the underlying type and value, where is the copied value of y
? We know it's:
- not in the interface itself
- not in
staticuint64s
(see Special values) as the value is greater than255
So where is it? It is in one of two locations, and we are about to take a look at the first one...
Next: On the stack