Using go run with an incompatible GOOS

18 views
Skip to first unread message

Quim Muntal

unread,
Nov 16, 2021, 3:55:46 AM11/16/21
to golang-tools
Hi all, I have a question regarding using x/tools/go/packages.

I'm doing a tool that creates a custom call graph for public functions in the given package[s]. In some cases the call graph depends on the GOOS/GOARCH values, but luckily go/packages already uses that env variables to parse the packages.

The problem I'm finding is that the tool execution fails when using go run together with a GOOS that is not the host OS, as the resulting binary cannot be executed. It works well if first building the tool with GOOS={host OS} and the executing it with GOOS={target OS}. 

The only solution I found is to accept an optional GOOS as argument which overwrites the GOOS env, but I'll like to hear your thoughts.

Also, when using go run the resulting binary will always(?) be executed in the host OS, so I think using GOHOSTOS instead of GOOS would make more sense.

Thanks,
Quim
Reply all
Reply to author
Forward
0 new messages