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
+46.9k Golang : Upload file from web browser to server
+32.3k Golang : All update packages with go get command
+19.7k Golang : How to read float value from standard input ?
+7.7k Golang : Find network service name from given port and protocol
+7.8k Golang : GMail API create and send draft with simple upload attachment example
+33.5k Golang : Upload and download file to/from AWS S3
+5k Golang : How to verify input is rune?
+16.3k Golang : [json: cannot unmarshal object into Go value of type]
+7.3k Swift : Convert (cast) Character to Integer?
+9.6k Golang : Replace a parameter's value inside a configuration file example
+16.8k Golang : Display list of time zones with GMT
+5.1k Golang : Convert Chinese UTF8 characters to Pin Yin