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
+22.7k Generate checksum for a file in Go
+8.9k Golang : Sort lines of text example
+15.1k Golang : Search folders for file recursively with wildcard support
+25.9k Golang : How to read integer value from standard input ?
+15.6k Golang : rune literal not terminated error
+4.8k Which content-type(MIME type) to use for JSON data
+4.6k Javascript : Detect when console is activated and do something about it
+11.2k Golang : How to determine a prime number?
+12.7k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+9.4k Golang : Temperatures conversion example
+6.2k Golang : Grab news article text and use NLP to get each paragraph's sentences
+24.5k Golang : Change file read or write permission example