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.4k Mac OSX : Get disk partitions' size, type and name
+10.3k Golang : Get local time and equivalent time in different time zone
+26.4k Golang : How to check if a connection to database is still alive ?
+6.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12k Golang : Encrypt and decrypt data with x509 crypto
+7.5k Golang : Lock executable to a specific machine with unique hash of the machine
+4.7k Nginx and PageSpeed build from source CentOS example
+13.9k Golang : Chunk split or divide a string into smaller chunk example
+12.3k Golang : HTTP response JSON encoded data
+21.4k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+4.4k Javascript : Access JSON data example