ryan961 / memo

https://github.com/ryan961/memo/issues
Creative Commons Zero v1.0 Universal
0 stars 0 forks source link

Go Build: Omitting dev dependencies in Go binaries #7

Open ryan961 opened 7 months ago

ryan961 commented 7 months ago

🤖 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.

//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:

  1. 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
  1. 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'

Related Links:

ryan961 commented 7 months ago

💡 Related Reading

📌 TODO: I plan to compile and organize related material in the future.