Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Last active August 15, 2025 07:06
Show Gist options
  • Select an option

  • Save wjkoh/5977a9f6b50f078d971bc35d605068b5 to your computer and use it in GitHub Desktop.

Select an option

Save wjkoh/5977a9f6b50f078d971bc35d605068b5 to your computer and use it in GitHub Desktop.
Go: Do you know `-trimpath`?

-trimpath

remove all file system paths from the resulting executable. Instead of absolute file system paths, the recorded file names will begin either a module path@version (when using modules), or a plain import path (when using the standard library, or GOPATH).

https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies

Without -trimpath, e.g., go run .

{
  "timestamp": "2025-08-15T15:50:30.180273+09:00",
  "severity": "INFO",
  "source": {
    "function": "github.com/cowork-ai/fred.NewHTTPServer.func1.1",
    "file": "/Users/wjkoh/fred/http.go",
    "line": 34
  },
  "message": "Starting an HTTP server..."
}

With -trimpath, e.g., go run -trimpath .

{
  "timestamp": "2025-08-15T15:50:56.45097+09:00",
  "severity": "INFO",
  "source": {
    "function": "github.com/cowork-ai/fred.NewHTTPServer.func1.1",
    "file": "github.com/cowork-ai/fred/http.go",
    "line": 34
  },
  "message": "Starting an HTTP server..."
}

Check out Go docker images: small and simple for more information. By the way, -ldflags=-s implies -ldflags=-w, so you need -s only according to https://pkg.go.dev/cmd/link#hdr-Command_Line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment