go

How can I remove absolute paths from the stack trace?


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

Solution

  • Since go 1.13, you can just use the -trimpath flag when you run go build (ref):

    go build -trimpath ...
    

    Before go 1.13...

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