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
+14.9k Golang : Get IP addresses of a domain name
+4.2k Golang : Pad file extension automagically
+8k Golang : Generate EAN barcode
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+18.5k Golang : Append content to a file
+16.5k Golang : How to log each HTTP request to your web server?
+6.3k Golang : How to stop user from directly running an executable file?
+12.6k Javascript : Prompt confirmation before exit
+14.3k Golang : invalid character ',' looking for beginning of value
+4.3k Golang : Issue HTTP commands to server and port example
+9.1k Golang : How to tokenize source code with text/scanner package?
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status