how to get the full path of current process' executable file
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Use package bitbucket.org/kardianos/osext.
It's providing function Executable()
that returns an absolute path to the current program executable.
It's portable between systems.
package main
import (
"bitbucket.org/kardianos/osext"
"fmt"
)
func main() {
filename, _ := osext.Executable()
fmt.Println(filename)
}
Even though I generally would agree with your observation, I do think there are valid use cases you can think of. For example when you want your executable to check for a config file at some default locations when it is being started without any options (and then one of those default locations being the application directory of course :)
Or when you want to cache some stuff (over restarts and time, so using /tmp wouldn't be sufficient) in a .gob file, it would also be nice if the executable could just place that .cache file besides the executable itself. This latter example could of course also be in a config file, but then you at least should be able to look for the config file (see my first example ;)
Or do you still consider this bad practice and so you would make it mandatory to specify the config file when calling the executable?
When I deploy a program to a Windows machine, it may be on the S: drive,may be on the D: drive, and I don't know where it should read a configurationfile when I compile it. So in fact, being able to get the executable's locationcan make it more capable. I could pass it the configuration on the command linebut that is awkward for windows "services". So while I know it isn't best practice in manyenvironments, there is a place for it, even if you haven't encountered it yourself.