📖 2 min read (~ 300 words).

Custom templates

go-swagger renders code from Go templates, and you can swap in your own. The contributed-templates example uses the built-in --template stratoscale option — a community template set that produces a different, interface-first shape optimized for testability.

Tip

Source: contributed-templates/stratoscale/. Generated with swagger generate server -A Petstore --template stratoscale (and the matching swagger generate client --template stratoscale).

What the template changes

Instead of the default’s per-operation handler func fields, the stratoscale template groups operations into interfaces — one per tag — that your code implements, and emits //go:generate mockery directives so those interfaces are trivially mockable in tests:

//go:generate mockery --name PetAPI --inpackage

// PetAPI
type PetAPI interface {
	// PetCreate Add a new pet to the store
	PetCreate(ctx context.Context, params pet.PetCreateParams) middleware.Responder
	// PetDelete Deletes a pet
	PetDelete(ctx context.Context, params pet.PetDeleteParams) middleware.Responder
	// PetGet Get pet by it's ID
	PetGet(ctx context.Context, params pet.PetGetParams) middleware.Responder
	// PetList List pets
	PetList(ctx context.Context, params pet.PetListParams) middleware.Responder
	// PetUpdate Update an existing pet
	PetUpdate(ctx context.Context, params pet.PetUpdateParams) middleware.Responder
	// PetUploadImage uploads an image
	PetUploadImage(ctx context.Context, params pet.PetUploadImageParams) middleware.Responder
}

Full source: contributed-templates/stratoscale/restapi/configure_petstore.go

Every handler is context-first (ctx context.Context, params …), matching the stratoscale client flavor. You provide one implementation per interface (PetAPI, StoreAPI, …) rather than assigning individual handler funcs.

When to use it

Reach for a custom template set when the default output doesn’t fit your codebase conventions — here, interface-based handlers plus generated mocks. Because it’s a whole template family, it changes server and client output consistently.

To go further, --template-dir points the generator at your own template directory, letting you override any individual template go-swagger ships.