Sobes.tech
Principal

What is an interface in Go, how does it look externally and internally?

sobes.tech AI

Answer from AI

In Go, an interface is a set of methods that describe behavior. Any type that implements all the methods of an interface is automatically considered its implementation.

Externally, an interface is declared as:

type Reader interface {
    Read(p []byte) (n int, err error)
}

Internally, an interface stores:

  • A method table — pointers to the functions of the methods.
  • A pointer to the specific object that implements these methods.

Thus, an interface is a kind of pointer to an object and a set of methods, which allows implementing polymorphism without explicit inheritance.