4

How can I remove absolute paths from the stack trace?

For example, now:

main.main()
    /home/userName/WORKSPACE/temp/mvps/main.go:16 +0xb2

And I want it to be something like this:

main.main()
    main.go:16 +0xb2
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
IvanP
  • 51
  • 3

1 Answers1

6

To remove the GOPATH prefix, add the following flags when you run go build (ref):

go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH ...

If GOPATH is not set in your environment, $(go env GOPATH) is still likely to work (thanks to Flimzy for pointing this out):

go build -gcflags=-trimpath=$(go env GOPATH) -asmflags=-trimpath=$(go env GOPATH) ...
MTCoster
  • 5,868
  • 3
  • 28
  • 49
  • i would say that this is something very wanted for those that must spare disk usage regarding logs in a server.. – Victor Dec 08 '18 at 15:07
  • @Victor It’s a surprisingly awkward solution - you’d think there would be a better way of doing this. I have a shell alias set up to include those every time. – MTCoster Dec 08 '18 at 15:10
  • 2
    is not awkward to my eyes @MTCoster. You may use a Makefile to build your project, placing the flags (and any aditional config in there) once for all. – Victor Dec 08 '18 at 15:15
  • How to do this in Go 1.14? All examples I found use this obsolete method. – Santiago May 18 '20 at 18:57