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
+6.9k Your page has meta tags in the body instead of the head
+9.4k PHP : Convert(cast) bigInt to string
+6k Golang : Example of how to detect which type of script a word belongs to
+8.2k Golang : interface - when and where to use examples
+17.6k Golang : Check if os.Stdin input data is piped or from terminal
+6.6k Golang : Multiplexer with net/http and map
+16.7k Golang : Generate thumbnails from images
+24.5k Golang : dial tcp: too many colons in address
+5.6k Golang : Takes a plural word and makes it singular
+36k Golang : Convert to io.ReadSeeker type
+7.5k Golang : Generate random Chinese, Japanese, Korean and other runes
+10k How to tell if a binary(executable) file or web application is built with Golang?