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
+4.7k Golang : A program that contain another program and executes it during run-time
+16.8k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+12.2k Golang : Get month name from date example
+8.2k Useful methods to access blocked websites
+4.6k Golang : How to pass data between controllers with JSON Web Token
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+12.1k Golang : calculate elapsed run time
+13k Golang : How to get a user home directory path?
+14.7k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+40.9k Golang : How to count duplicate items in slice/array?
+5.6k List of Golang XML tutorials
+23.8k Golang : Call function from another package