Answer the question
In order to leave comments, you need to log in
How to get the absolute path of a running program in Go?
Hello. Faced such an interesting problem and I can not find the answer to such a simple question in the internet. Or rather, I found it, but it does not work for me.
The point is to get the absolute path to the running server in order to load the config.json file that lies nearby.
Here is the code itself:
rootDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
glog.Infoln("rootDir:", rootDir)
Answer the question
In order to leave comments, you need to log in
my build goes through make (go build is always done, then the compiled binary is launched), and the path to the config is passed through parameters (flags)
as an option, you can look in several places, and add os.Getwd() to rootDir
bitbucket.org/kardianos/osext
folder, _ := osext.ExecutableFolder()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question