3

I have read this article which indicates a double tuple structure, but it is unfortunately light on implementation details, which is what I am looking for.

So... how are interfaces implemented in Go?

Pure guesswork on my part:

  • It cannot be a v-table in the C++ sense, because the interface is implicit - a struct doesn't know it conforms to an interface when compiled, only when passed to a function taking an interface does this actually happen.
  • I don't think it can be static polymorphism - every function that takes an interface parameter would have to be compile-time aware of every type that might be passed to it and I don't think that's true either.

What else is left?

9
  • 2
    I had the same question as you and wrote down my findings here: lukasatkinson.de/2018/interface-dispatch/#fat-pointers (but might be partially outdated by now). Yes there's a vtable, but its tracked outside of the object, and it may be created lazily by the language runtime.
    – amon
    Commented Apr 27, 2023 at 15:36
  • Interesting! I assume that in turn this (edit: the fat pointer you write about) implies that a function using interfaces always does pass-by-reference? (Not that this would be a loss compared to any other language I know but that wasn't what I thought was going on in go)
    – SRNissen
    Commented Apr 27, 2023 at 15:43
  • Based on what I found with a quick search, Go passes references by value similar to how it works in Java and Python.
    – JimmyJames
    Commented Apr 27, 2023 at 15:54
  • 3
    Sometimes it seems like every single question is off topic for here. Voted to reopen.
    – pjc50
    Commented Apr 28, 2023 at 8:33
  • 1
    Go ahead - though you may also want to look at this article that I got when I asked the same question in this reddit thread.
    – SRNissen
    Commented Apr 29, 2023 at 10:25

1 Answer 1

2

In Swift, there are (kind of) objects that represent interfaces. Say an interface has three functions. Then the interface object has three function pointers and a bit of storage. When you pass an object to a function expecting an interface, an interface object is created, three function pointers are stored, and the data is stored. I think (from memory) that for class instances a reference is stored, for small structs the contents of the struct, and for large structs a pointer is allocated.

Sounds a reasonable way to do this, so Go might do the same. You can convert such an object back to the original type if you know the type. A function using the interface object needs no knowledge of the actual type. Called an “existential interface”, so an interface that actually exists as an object.

1
  • Would you mind if I edit in the precise terms for these? In Swift, these are called existential containers. They're 4 words in size: a pointer to the type metadata (called a protocol witness table, which has the function pointers, among other things) and 3 words of storage for the value. Small enough values are allocated inline, otherwise the first word is just a pointer to heap-allocated storage. See stackoverflow.com/a/68024022/3141234 for more detail
    – Alexander
    Commented Dec 29, 2023 at 18:19

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.