Project Organization and Style
Small Interfaces
Go interfaces usually work best when consumers define only the behavior they need.
Small Interfaces
small_interfaces.go
package main
import "fmt"
func main() {
var methodCount =
interfaceName := "Reader"
if methodCount > 1 {
interfaceName = "Repository"
}
focused := methodCount <= 2
testDouble := "simple fake"
if !focused {
testDouble = "large mock"
}
fmt.Println("interface=", interfaceName)
fmt.Println("methods=", methodCount)
fmt.Println("focused=", focused)
fmt.Println("test_double=", testDouble)
}
package main
import "fmt"
func main() {
var methodCount =
interfaceName := "Reader"
if methodCount > 1 {
interfaceName = "Repository"
}
focused := methodCount <= 2
testDouble := "simple fake"
if !focused {
testDouble = "large mock"
}
fmt.Println("interface=", interfaceName)
fmt.Println("methods=", methodCount)
fmt.Println("focused=", focused)
fmt.Println("test_double=", testDouble)
}
package main
import "fmt"
func main() {
var methodCount =
interfaceName := "Reader"
if methodCount > 1 {
interfaceName = "Repository"
}
focused := methodCount <= 2
testDouble := "simple fake"
if !focused {
testDouble = "large mock"
}
fmt.Println("interface=", interfaceName)
fmt.Println("methods=", methodCount)
fmt.Println("focused=", focused)
fmt.Println("test_double=", testDouble)
}
interfaces
Small consumer-owned interfaces keep packages decoupled and tests simple.