Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

go - Packages disabled by build tags still linked into binary

I use build tags to disable one feature, such as:

source

  • abc_enable.go
// +build abc
package main

import (
  "big-package"
  "bigbig"
)
...
  • abc_disable.go
// +build !abc

// empty

build cmd

  • enable: go build --tags abc
  • disable: go build

this method works always.

problem

but I found build binary size keeps big when abc disabled.

  • keep abc_enable.go, --tags abc. size up to 11M
  • keep abc_enable.go, no tags. size up to 11M
  • remove abc_enable.go by hand, size down to 5M

is there any link options or build options to fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Build constraints

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

That means that // +build abc in your original code snippet is not considered a build constraint. To fix this you have to add a blank line above of the package clause.

// +build abc

package main

import (
  "big-package"
  "bigbig"
)
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...