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,
structis a collection of data fields,interfaceis a collection of methods
go interfaces are implemented implicitly:
- in the below example, when we pass
StructNameas a [[go-struct-method#|receiver]] inInterfaceName’s method definition, it implicitly means thatStructNameis implementingInterfaceName
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]]