Structural Patterns in Go

Adapter, Decorator, Facade

Implement Adapter, Decorator, and Facade patterns in Go by defining interfaces and creating wrapper structs that compose existing types.

Go does not have built-in structural design patterns like Adapter, Decorator, or Facade; you implement them using interfaces and composition. Define an interface for the behavior you need, then create a struct that wraps the target type and implements that interface to adapt or decorate it.

type Logger interface {
    Log(msg string)
}

type StdLogger struct{}
func (s *StdLogger) Log(msg string) { fmt.Println(msg) }

type DecoratedLogger struct {
    Logger
    prefix string
}
func (d *DecoratedLogger) Log(msg string) {
    d.Logger.Log(d.prefix + ": " + msg)
}