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
+10.2k Golang : http.Get example
+5.7k Golang : Append and add item in slice
+9.9k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+18.7k Golang : Read directory content with filepath.Walk()
+3k Google : Block or disable caching of your website content
+6.3k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+3.2k Python : Convert(cast) string to bytes example
+6.5k PHP : Get coordinates latitude/longitude from string
+14k Golang : Populate dropdown with html/template example
+10.9k Golang : Get digits from integer before and after given position example
+8.1k Golang : Arithmetic operation with numerical slices or arrays example
+9k Golang : Handle or parse date string with Z suffix(RFC3339) example