Skip to content

Build without MsgPack

MsgPack (MessagePack) is a compact binary serialization format — think of it as a faster, smaller alternative to JSON. Gin includes MsgPack rendering and binding support by default, which means your application can accept and return MsgPack-encoded data out of the box using c.Bind() and c.Render() with the appropriate content type.

However, many applications only use JSON and never need MsgPack. In that case, the MsgPack dependency adds unnecessary weight to your compiled binary. You can strip it out with the nomsgpack build tag.

Building without MsgPack

Pass the nomsgpack tag to go build:

Terminal window
go build -tags=nomsgpack .

This also works with other Go commands:

Terminal window
go run -tags=nomsgpack .
go test -tags=nomsgpack ./...

What changes

When you build with nomsgpack, Gin excludes the MsgPack rendering and binding code at compile time. This has a few practical effects:

  • The compiled binary is smaller because the MsgPack serialization library is not linked in.
  • Any handler that attempts to render or bind MsgPack data will no longer work. If you use c.ProtoBuf() or other non-MsgPack renderers, those are unaffected.
  • All JSON, XML, YAML, TOML, and ProtoBuf features continue to work normally.

Verifying the result

You can confirm the binary size reduction by comparing builds:

Terminal window
# Standard build
go build -o gin-app .
ls -lh gin-app
# Build without MsgPack
go build -tags=nomsgpack -o gin-app-nomsgpack .
ls -lh gin-app-nomsgpack

The exact savings depend on your application, but removing MsgPack typically shaves a small amount off the final binary. For more background, see the original pull request.