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
+11.6k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+10.7k Golang : Flip coin example
+15.4k Golang : Get timezone offset from date or timestamp
+9.2k Golang : Gonum standard normal random numbers example
+5.4k Swift : Convert string array to array example
+6.1k Golang : Compound interest over time example
+19.9k Golang : Archive directory with tar and gzip
+9.4k Golang : How to get garbage collection data?
+9.1k Golang : How to use Gorilla webtoolkit context package properly
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+7.3k Golang : Dealing with postal or zip code example
+18.1k Golang : Qt image viewer example