Get the current process (executable) name in Go?
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.