Golang : get the current working directory of a running program
Problem :
You are looking for way to get the folder/directory of the running program.
Solution :
Use https://bitbucket.org/kardianos/osext ExecutableFolder()
function. For example :
package main
import (
"bitbucket.org/kardianos/osext"
"fmt"
)
func main() {
// get the current folder of the running program
path, err := osext.ExecutableFolder()
if err != nil {
fmt.Println(err)
}
fmt.Println("Program is executing at folder :", path)
}
Sample output :
go run executabledir.go
Program is executing at folder : /tmp/go-build113823238/command-line-arguments/_obj/exe/
See also : Golang : Get current file path of a file or executable
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7k How to let Facebook Login button redirect to a particular URL ?
+8.2k Golang : Get all countries phone codes
+27.8k PHP : Convert(cast) string to bigInt
+6.4k Golang : How to get capacity of a slice or array?
+22.6k Golang : How to read JPG(JPEG), GIF and PNG files ?
+5.9k Golang : Find change in a combination of coins example
+14.1k Golang : concatenate(combine) strings
+7.7k Android Studio : AlertDialog to get user attention example
+8.3k Golang : Metaprogramming example of wrapping a function
+19.2k Mac OSX : Homebrew and Golang
+11.2k Golang : Roll the dice example
+6.4k Golang : Calculate US Dollar Index (DXY)