what’s an interface

Note

interfaces in go is just a set of method signatures (collection of method signature)

  • how to understand signature here?
    • method signature is the name, argument types, and return type of a method
type shape interface {
  area(int, int) int // this is a method signature
  perimeter() float64
}
  • in other words, struct is a collection of data fields, interface is a collection of methods

go interfaces are implemented implicitly:

  • in the below example, when we pass StructName as a [[go-struct-method#|receiver]] in InterfaceName’s method definition, it implicitly means that StructName is implementing InterfaceName
type InterfaceName interface {
	methond_name() return_type
}
type StructName struct { ...some fields... }
func (var_name StructName) method_name() return_type { ... }

up

[[go-struct-method#|#^func-def]]

down

reference