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
+10k Golang : 2 dimensional array example
+14.7k Golang : XML to JSON example
+7k Mac OSX : Get a process/daemon status information
+3k Golang : Experimental Jawi programming language
+38.9k Golang : How to check if a string contains another sub-string?
+15.1k Golang : [json: cannot unmarshal object into Go value of type]
+10.9k Golang : http.Get example
+6.2k Golang : Auto-generate reply email with text/template package
+4.8k Golang : How to validate ISBN?
+3.9k Golang : If else example and common mistake
+6.8k Golang : automatically figure out array length(size) with three dots