2

In this blog post the author states that:

Structs can implement interfaces, of course, so in general you tend to treat these as the same thing. But when you're dealing with a struct, you might be passing by reference, in which the type is *myStruct, or you might be passing by value, in which the type is just myStruct. If, on the other hand, the thing you're dealing with is "just" an interface, you never have a pointer to it -- an interface is a pointer in some sense. It can get confusing when you're looking at code that is passing things around without the * to remember that it might actually "be a pointer" if it's an interface rather than a struct.

In what sense can Go's interface be considered a pointer? Please provide some examples.

syntagma
  • 23,346
  • 16
  • 78
  • 134
  • 1
    http://stackoverflow.com/a/23148998/6309 can illustrates a bit the pointer included in an interface. – VonC Jan 07 '15 at 19:54

1 Answers1

3

This quote is strange. I think he wants to say something in the line of this: Methods on pointer receivers are a clear indication that the method may modify the receiver as the method is invoked on the original struct and not on a copy. This clear indication of "You work on pointers here, this might modify the original!" is lost once you wrap a struct in an interface value: You may pass around copies of the interface value but all these copies wrap the same struct.

Volker
  • 40,468
  • 7
  • 81
  • 87
  • Also, it's often redundant to pass a pointer to an `interface` type to avoid copying--it's already a small (two-word) value. If it helps, here are [Russ Cox's post on how interface values are stored](http://research.swtch.com/interfaces) and [an answer covering what other Go types are implemented with pointers](http://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values/23551970#23551970). – twotwotwo Jan 07 '15 at 20:59
  • 4
    @twotwotwo: Correct. One should "never" pass interface values via pointers with "never" in the sense of "until you have to and you will know when you have to". – Volker Jan 07 '15 at 21:02