This article discusses the methods to exclude development dependencies from Go binaries to reduce their size. The author explains how to use build tags to separate dev-only packages and prevent them from being included in the final build. This technique is useful for optimizing Go binary size, which is beneficial for deployment and distribution. Practical examples and commands are provided to demonstrate how to apply build tags in Go projects.
🖇️ Details
Title: Omitting development dependencies from your Go binaries
A quite commendable little trick that has greatly benefited my daily coding (I wish I had known about this sooner 😤). For example, I frequently utilize the spew to print detailed variable data.
//go:build tools
// Package tools is used to track binary dependencies with go modules
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
package tools
import (
// Dev dependencies
_ "github.com/davecgh/go-spew/spew"
)
In addition, there are two other little tricks:
If you want to include the dev dependencies in your binary, you can pass the tools tag while building the binary.
go build --tags tools main.go
You can check if a dependency is included in the binary file with the command below.
go tool nm main | grep -Ei 'golangci-lint|gofumpt'
🤖 AI Summary
This article discusses the methods to exclude development dependencies from Go binaries to reduce their size. The author explains how to use build tags to separate dev-only packages and prevent them from being included in the final build. This technique is useful for optimizing Go binary size, which is beneficial for deployment and distribution. Practical examples and commands are provided to demonstrate how to apply build tags in Go projects.
🖇️ Details
🔖 Note
A quite commendable little trick that has greatly benefited my daily coding (I wish I had known about this sooner 😤). For example, I frequently utilize the
spew
to print detailed variable data.In addition, there are two other little tricks:
Related Links: