HomeToolsAbout

Interface vs Struct

interface vs struct

A pointer to a struct and a pointer to an interface are not the same.

An interface can store either a struct directly or a pointer to a struct.

  • When interface points to a struct, you do not use a pointer to the interface.
  • You use the interface directly.
type Fooer interface { Dummy() } type Foo struct{} func (f Foo) Dummy() {} func main() { var f1 Foo var f2 *Foo = &Foo{} DoFoo(f1) DoFoo(f2) } func DoFoo(f Fooer) { fmt.Printf("[%T] %+v\n", f, f) } /* output */ // [main.Foo] {} // [*main.Foo] &{}
AboutContact