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
+37.5k Golang : Comparing date or timestamp
+13.7k Golang : convert(cast) string to float value
+9.9k Golang : Test a slice of integers for odd and even numbers
+13.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+12.4k Golang : Sort and reverse sort a slice of bytes
+12.1k Golang : Print UTF-8 fonts on image example
+18.2k Golang : Example for RSA package functions
+5.7k Golang : Generate multiplication table from an integer example
+20.2k Android Studio : AlertDialog and EditText to get user string input example
+42.9k Golang : Get hardware information such as disk, memory and CPU usage
+5.8k nginx : force all pages to be SSL
+12.1k Golang : Encrypt and decrypt data with x509 crypto