Interfaces
An interface contains a collection of method signatures.
Interfaces are implemented implicitly. There is no implements
keyword.
// Declaration
type name interface{}
type Shape interface {
Area() float64
}
// Struct for implementation
type rectangle struct {
width, height float64
}
func (r rectangle) Area() float64 {
return r.Width * r.Height
}
The empty interface
An empty interface (interface{}
or any
with Go 1.18+) is implemented by all types.
Type assertions
Access the underlying concrete type of an interface with type assertion.
var i interface{} = "text"
// panic if type is wrong
var s string = i.(string)
// check type
s, ok := i.(string)
Type switches
Instead of the specific type, the keyword type
is used.