Interfaces
Interfaces are one of the most powerful constructs in Go, so it’s very important to understand what they do and when you can use them. From a purely theoretical point of view, interfaces are an abstract type. They do not contain implementation details but define a set of behaviors through method signatures.
If a Go type defines all method signatures declared by an interface, this Go type implements that interface implicitly, with no explicit declaration. This is how Go deals with common behaviors exhibited by more than one type and what other languages often express through object inheritance.
Network Automation Example
To introduce the idea, we use a contrived network automation example. Let’s say we are developing a Go package to deal with common tasks across different network devices. We model a Cisco IOS XE device as CiscoIOS
type with two fields — one that identifies the hostname of a device (Hostname
) and another one that identifies the underlying...