How to Implement the Adapter Pattern in Go

Implement the Adapter Pattern in Go by wrapping an incompatible type in a struct that implements your target interface.

Implement the Adapter Pattern in Go by defining a target interface, creating an adapter struct that embeds the incompatible type, and implementing the target interface methods on the adapter.

type Target interface {
	DoWork() string
}

type LegacyService struct{}

func (l *LegacyService) Process() string {
	return "processed"
}

type Adapter struct {
	*LegacyService
}

func (a *Adapter) DoWork() string {
	return a.Process()
}

func main() {
	var t Target = &Adapter{LegacyService: &LegacyService{}}
	fmt.Println(t.DoWork())
}