Skip to content

Build Tags

Go build tags (also called build constraints) are directives that tell the Go compiler to include or exclude files during compilation. Gin uses build tags to let you swap out internal implementations or disable optional features at compile time, without changing any application code.

This is useful in several scenarios:

  • Performance optimization – Replace the default encoding/json package with a faster third-party encoder to speed up JSON serialization in your API.
  • Binary size reduction – Strip out features you do not use, such as MsgPack rendering, to produce a smaller compiled binary.
  • Deployment tuning – Choose different encoders for different environments (e.g., a high-throughput production build vs. a standard development build).

Build tags are passed to the Go toolchain with the -tags flag:

Terminal window
go build -tags=<tag_name> .

You can combine multiple tags by separating them with commas:

Terminal window
go build -tags=nomsgpack,go_json .
Tag Effect
go_json Replaces encoding/json with go-json
jsoniter Replaces encoding/json with jsoniter
sonic avx Replaces encoding/json with sonic (requires AVX CPU instructions)
nomsgpack Disables MsgPack rendering support

The pages below cover each build tag in detail: