How the main func started?

95 views
Skip to first unread message

dc0d

unread,
Sep 17, 2016, 9:28:16 AM9/17/16
to golang-nuts
How to find out if the main function is started by executing the compiled binary or by go run?

By examining os.Args it can be seen at the first case (running the app) we will have the binary name as the first argument without any path and when it is started using go run, we will have something like /tmp/go-build899137799/command-line-arguments/_obj/exe/appname as the first argument.

Is this a reliable way for performing this task? To assume if the path is similar to this - for example using regex - it is running using go run?

Motivation:
The go run mode is used for some code generation - I've not tested if it works with //go:generate go run main.go or not; yet.

peterGo

unread,
Sep 17, 2016, 10:02:47 AM9/17/16
to golang-nuts
dc0d,

It's implementation dependendent. Be explicit. For example, pass a flag argument,

//go:generate go run main.go -gogenerate

Peter
Reply all
Reply to author
Forward
0 new messages