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
+3.4k Golang : Null and nil value
+21.3k Golang : Copy directory - including sub-directories and files
+5.2k Golang : Convert int(year) to time.Time type
+3.8k Golang : Qt get screen resolution and display on center example
+4.6k Golang : Fix - does not implement sort.Interface (missing Len method)
+2.5k Golang : Accessing dataframe-go element by row, column and name example
+2.1k Which content-type(MIME type) to use for JSON data
+2.8k Golang : Rot13 and Rot5 algorithms example
+4.4k SSL : How to check if current certificate is sha1 or sha2 from command line
+3.6k Golang : Validate credit card example
+3.9k Golang : Apply Histogram Equalization to color images
+6.4k Golang : Get all local users and print out their home directory, description and group id