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
+6k Golang : Null and nil value
+8.6k Golang : How to check if a website is served via HTTPS
+8k Golang : Generate random Chinese, Japanese, Korean and other runes
+10.3k Golang : GTK Input dialog box examples
+4.8k List of Golang XML tutorials
+22.2k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+4k Google : Block or disable caching of your website content
+8.3k Golang : Find correlation coefficient example
+11.9k Golang : Increment string example
+14.7k Golang : Read a file line by line
+6.1k Web : How to see your website from different countries?
+12.9k Golang : Check if a file exist or not