The interface type
When you talk to people who have been doing Go for a while, they almost always list the interface as one of their favorite features of the language. The concept of interfaces in Go, similar to other languages, such as Java, is a set of methods that serves as a template to describe behavior. A Go interface, however, is a type specified by the interface{}
literal, which is used to list a set of methods that satisfies the interface. The following example shows the shape
variable being declared as an interface:
var shape interface { area() float64 perim() float64 }
In the previous snippet, the shape
variable is declared and assigned an unnamed type, interface{area()float64; perim()float64}
. Declaring variables with unnamed interface ...
Get Go: Design Patterns for Real-World Projects now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.