Removing File Paths from TEXT Directives in Go Binaries
To eliminate path information from the executable created by go build, use -trimpath flags.
Use -trimpath Flags:
Append -gcflags=-trimpath=/Users/myuser/dev/go/src and -asmflags=-trimpath=/Users/myuser/dev/go/src to the go build command:
CGO_ENABLED=0 go build -v -a -ldflags="-w -s" \ -gcflags=-trimpath=/Users/myuser/dev/go/src \ -asmflags=-trimpath=/Users/myuser/dev/go/src \ -o ./fooapi spikes/mongoapi.go
Explanation:
Passing -trimpath removes the prefix provided from recorded source file paths. This action eliminates path information from elf binaries.
Verification:
Use go tool objdump to confirm the removal:
$ go tool objdump ./fooapi . . TEXT main.init(SB) api/spikes/mongoapi.go
Caution Regarding strip:
Although strip has been reportedly fixed, some controversies remain within the Go community. Unknown and unpredictable bugs may still occur. Exercise caution when using strip.
The above is the detailed content of How Can I Remove File Paths from Go Binaries Using `-trimpath`?. For more information, please follow other related articles on the PHP Chinese website!