Go Kit is a toolkit for building microservices in Go that provides a consistent, opinionated structure for handling transport, middleware, and service interfaces, though it is now in maintenance mode and not actively developed. You use it by defining a pure Go service interface, implementing it, and then wrapping that implementation with transport-specific endpoints (like gRPC or HTTP) and middleware for cross-cutting concerns.
Start by defining your service interface and its implementation in a pure Go package, keeping business logic free of transport dependencies. Then, create an endpoint that translates transport-specific requests into calls to your service interface. Finally, wire everything together in your main function, injecting middleware for logging, tracing, and timeouts.
Here is a minimal example of defining a service and its endpoint:
// service.go
type Greeter interface {
Greet(ctx context.Context, name string) (string, error)
}
type greeter struct{}
func (g greeter) Greet(ctx context.Context, name string) (string, error) {
return fmt.Sprintf("Hello, %s", name), nil
}
// endpoint.go
func MakeGreetEndpoint(svc Greeter) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(GreetRequest)
resp, err := svc.Greet(ctx, req.Name)
return GreetResponse{Message: resp}, err
}
}
To run the service with HTTP transport and middleware, you wire it up in main.go:
func main() {
svc := greeter{}
endpoint := MakeGreetEndpoint(svc)
// Add middleware (logging, tracing, etc.)
endpoint = middleware.Log(endpoint)
endpoint = middleware.Timeout(5 * time.Second)(endpoint)
// Create HTTP handler
httpHandler := http.NewServer(endpoint, nil, nil)
// Start server
log.Fatal(http.ListenAndServe(":8080", httpHandler))
}
While Go Kit offers a robust pattern for decoupling transport from business logic, be aware that the project is no longer receiving new features. For new greenfield projects, consider alternatives like go-kit's spiritual successors or frameworks like buf with grpc-gateway, which offer similar architectural benefits with active community support. If you must use Go Kit, stick to the stable versions and rely heavily on its middleware stack to handle observability and resilience, as these are its strongest features. The key is to strictly separate your service interface from the transport layer, ensuring your core logic remains testable and transport-agnostic.