14

Citing the golang wiki (https://github.com/golang/go/wiki/MethodSets#interfaces):

"The concrete value stored in an interface is not addressable, in the same way, that a map element is not addressable."

The question of map values not being addressable is explained here: Why are map values not addressable?

However, it is not clear regarding the interface. Why are they not addressable? Is this because of some hard design assumption?

9
  • Possible duplicate of Types of Go struct methods that satisfy an interface.
    – icza
    Commented Feb 14, 2018 at 15:27
  • 2
    What good would a copy being addressable do, besides add more cognitive overhead to the language? Addressability is already taken care of by pointer values.
    – JimB
    Commented Feb 14, 2018 at 15:31
  • Well, it would allow satisfying interface by pointer receivers...
    – Lemurata
    Commented Feb 14, 2018 at 16:13
  • 1
    @Lemurata Yes, and if the pointer method would require to modify the receiver, any modification it would do would be lost, and you would scratch your head twice as hard why it doesn't work.
    – icza
    Commented Feb 14, 2018 at 16:15
  • @icza I would like to allow pointer receiver to modify the original structure not a copy. I don't understand why its impossible.
    – Lemurata
    Commented Feb 14, 2018 at 16:26

4 Answers 4

43

Why isn't a non-pointer value stored in an interface addressable? This is an excellent question, and the answer explains why an interface containing a non-pointer value can't be the receiver for a method with a pointer receiver, leading to the dreaded error:

<type> does not implement <interface> (<name> method has pointer receiver)

tl;dr

A non-pointer value stored in an interface isn't addressable to maintain type integrity. For example, a pointer to A, which points to a value of type A in an interface, would be invalidated when a value of a different type B is subsequently stored in the interface.

Because a non-pointer value stored in an interface isn't addressable, the compiler can't pass its address to a method with a pointer receiver.

Long answer

The answers I've seen online don't make much sense. For instance, this article says:

The reason is that the value in an interface is in a hidden memory location, and so the compiler can’t automatically get a pointer to that memory for you (in Go parlance, this is known as being “not addressable”).

It's true that the value stored in an interface is not addressable, but as far as I can see it's not because its stored in "a hidden memory location".

Another common answer is:

When an interface value is created, the value that is wrapped in the interface is copied. It is therefore not possible to take its address, and even if you did, using a pointer to the interface value would have unexpected effects (ie. unable to alter the original copied value).

This makes no sense, since a pointer to a value copied into an interface would be no different than a pointer to a value copied into a concrete type; in both cases you can't alter the original copied value through the pointer to the copy.

So why isn't a value stored in an interface addressable? The answer lies in the follow-on implications if it were addressable.

Let's say you have an interface, I, and two types, A and B, which satisfy that interface:

type I interface{}
type A int
type B string

Create an A and store it in an I:

func main() {
    var a A = 5
    var i I = a
    fmt.Printf("i is of type %T\n", i)

Let's pretend we could take the address of a value stored in an interface:

    var aPtr *A
    aPtr = &(i.(A)) // not allowed, but if it were...

Now create a B and store it in i:

    var b B = "hello"
    i = b
    fmt.Printf("i is of type %T, aPtr is of type %T\n", i, aPtr)
}

Here's the output:

i is of type main.A
i is of type main.B, aPtr is of type *main.A

After putting a B into i, what is aPtr pointing to? aPtr was declared as pointing to an A, but t now contains a B, and aPtr is no longer a valid pointer to A.

This, however is permitted:

    var aPtr *A
    var a2 A = i.(A)
    aPtr = &a2

Because the second line makes a copy of the value in i.(A), and aPtr does not point to i.(A).

So, why can't an interface containing a non-pointer value be the receiver for a method with a pointer receiver? Because a non-pointer value stored in an interface isn't addressable, so the compiler can't pass its address to a method with a pointer receiver.

4
  • Here's some code in the playground illustrating this: play.golang.org/p/eeaYzqZsmOs
    – WPWoodJr
    Commented Feb 20, 2018 at 15:49
  • 3
    Thanks for explaining instead of stating "you should ask another question", like the others do.
    – Lemurata
    Commented Feb 26, 2018 at 17:04
  • 1
    You don't have to make your reasoning for non-addressability of interface values. Go doesn't let it because it's an implementation detail and that's why Go spec disallows it. So different Go compilers (and versions) are free to choose whatever semantics they want for interface values. Interfaces were not always two-word structures. Commented Sep 2, 2019 at 14:28
  • 2
    "it's an implementation detail" with serious consequences as noted in my post
    – WPWoodJr
    Commented Sep 3, 2019 at 14:36
3

I think the answer is "because Go does not have references". If you call Foo(x) you know x won't be modified; if you call Foo(&x), you expect that it might. If what you request would be possible (in particular, if you want the value itself to be addressed, not the copy made in the interface), then that would break down:

func Bar() {
    b := 42
    Foo(b)
    fmt.Println(b)
}

func Foo(v interface{}) {
    // Making up syntax now
    p = takeAddressAs(v).(*int)
    *p = 23
}

Note, that it is possible (from a technical perspective) to address the copy stored in an interface and it definitely would be possible to build a language in which it would be allowed to modify the original value too (like you seem to want): This is basically how Python works. You could just make v = x be syntactic sugar for v = &x when v is of interface type. But that would add reference values to Go, which it intentionally lacks.

I think the root cause of the confusion here is that Go has syntactic sugar to call b.Foo(), even if Foo has a pointer receiver (as long as b is addressable). It's reasonable to be confused that you can call b.Write() but you can't do fmt.Fprintln(b, 42). Arguably, Go should just not have that sugar and instead require you to explicitly do (&b).Write or just make it a pointer to begin with (using, e.g. b := new(bytes.Buffer) instead of var b bytes.Buffer). But the answer to that is a) it's terribly convenient and b) it didn't seem that unexpected, that b.Foo() might modify b.

tl;dr is: a) Go does not have references, b) there is no technical reason it couldn't have them (contrary to the other answers' claims) but c) Go decided not to want them.

1
  • 1
    This question ("Why value stored in an interface is not addressable in Golang") is interesting because of an inconsistency in how Go handles pointer method receivers. If such a method is called with a non-pointer concrete value, Go will pass the receiver a pointer to the value, but not in the case where the value is stored in an interface. There can be no pointer to a reified value in an interface, because that could compromise type safety.
    – WPWoodJr
    Commented Feb 20, 2018 at 15:38
0

An interface in Go is the definition of methods that a type must provide if it is to be used as a type of the interface.

The code that uses an interface isn't concerned with how the implementing type does this under the covers, it just cares that the interface methods are satisfied.

As an example, lets suppose I have a simple logger interface that my code uses:

type Logger interface {
    Printf(format string, args ...interface{})
    Errorf(format string, args ...interface{})
}

I can use this interface all through my code. I'll need to supply an implementation of Logger where it's required. There may be several implementations. I may have a FileLogger that writes to a file. Should my code be able to access, for example, the File property in the FileLogger?

I may have a ConsoleLogger and a SockerLogger and a LogglyLogger all of which satisfy the interface. But it makes no sense in any of these to even have a File property. If my code that uses Logger interface was interested in the underlying File, what would it do when using these other Logger implementations?

If you need to access the underlying data in an interface implementation, you are probably using interfaces incorrectly.

A way around this is perhaps using Getter/Setters on the interface? If you really need access to a value from the underlying structure, make that part of the interface. In the example above a GetFile() won't make much sense but perhaps there are other situations where all implementations of an interface share a common attribute e.g. GetLen() and SetLen()?

0

Helpful info in go doc:https://golang.org/doc/faq#different_method_sets

Even in cases where the compiler could take the address of a value to pass to the method, if the method modifies the value the changes will be lost in the caller. As an example, if the Write method of bytes.Buffer used a value receiver rather than a pointer, this code:

var buf bytes.Buffer
io.Copy(buf, os.Stdin)

would copy standard input into a copy of buf, not into buf itself. This is almost never the desired behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.