开发者

Get the current process (executable) name in Go?

开发者 https://www.devze.com 2023-02-08 03:59 出处:网络
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.

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

精彩评论

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