Interfaces
Interfaces in Go (and most languages that offer interfaces) are:
- A set of method signatures
package main
type Runner interface {
SetStatement(stmt string)
Run() error
}
Types implement an interface by implicitly implementing it’s methods. No need to declare that your Type implements an interface. The type implements an interface if it simply implements it’s methods.
Pointers and functions implement interfaces as they are Types themselves.
package main
type Runner interface {
SetStatement(stmt string)
Run() error
}
type CassandraRunner struct {
Config string
}
func (cr *CassandraRunner) SetStatement(stmt string) {
// do something
}
func (cr *CassandraRunner) Run() {
// do something
}
Example Code
Resources
- Effective Go: Interfaces
- Go by Example: Interfaces
- Best Practices / Patterns