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
+11.1k Golang : Replace a parameter's value inside a configuration file example
+26.2k Golang : How to read integer value from standard input ?
+18.1k Golang : How to make a file read only and set it to writable again?
+19.6k Golang : Display list of time zones with GMT
+30.1k Golang : Get and Set User-Agent examples
+13.4k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+9.9k Golang : Find correlation coefficient example
+16.9k Golang : Gzip file example
+18.3k Golang : Convert IPv4 address to decimal number(base 10) or integer
+26.1k Golang : Daemonizing a simple web server process example
+5.6k Golang *File points to a file or directory ?
+9.6k Facebook : Getting the friends list with PHP return JSON format