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
+6.1k Golang : Generate Datamatrix barcode
+4.3k Golang : Missing Subversion command
+23.2k Golang : Find files by extension
+3.9k Golang : What is StructTag and how to get StructTag's value?
+4.8k Linux : How to fix Brother HL-1110 printing blank page problem
+10.7k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+20.7k Golang : Print out struct values in string format
+7.1k Golang : Read file with ioutil
+7.5k Golang : Compare files modify date example
+3.5k PHP : Fix Call to undefined function curl_init() error
+4.2k Fix fatal error: evacuation not done in time problem