开发者

Get the current process (executable) name in Go?

What I am looking for here is the equivalent of C's argv[0].

The flag pac开发者_开发问答kage only gives access to command line arguments, but not the executable name.

While one can get the process with Getpid(), I haven't found something that will give me access to the whole command line. The syscall command GetCommandLine() seems only to be available on Windows.


The traditional argv[0] in C is available in os.Args[0] in Go. The flags package simply processes the slice os.Args[1:]


A better way as follows:

filename:=filepath.Base(os.Args[0])

This will present only the application name and remove the path for you.


Since Go 1.8, the answer is os.Executable(). Similar to other languages, there is also os.Args[0]. One important distinction is that os.Executable() is guaranteed to return an absolute path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜