How to Build an Extensible Application with Go

Build extensible Go apps by defining interfaces for core behaviors, allowing new implementations to be added without modifying existing client code.

Build extensible Go applications by defining interfaces for core behaviors and implementing them with concrete types, allowing you to swap implementations without changing client code. This approach, demonstrated in the net/rpc package's transition from gob to JSON, relies on defining a ServerCodec interface that abstracts the wire format, enabling new formats like jsonServerCodec to be added simply by implementing the interface methods.

type ServerCodec interface {
	ReadRequestHeader(*Request) error
	ReadRequestBody(interface{}) error
	WriteResponse(*Response, interface{}) error
	Close() error
}

func sendResponse(sending *sync.Mutex, req *Request, reply interface{}, enc ServerCodec, errmsg string) {
	// Logic uses enc interface, agnostic of gob or json implementation
}