Golang : Get path name to current directory or folder
Problem :
You need to get the path name of the current directory where the executable is residing. How to do that in Golang?
Solution :
Use the os.Getwd()
function.
For example :
package main
import (
"fmt"
"os"
"strings"
)
func main() {
dir, _ := os.Getwd()
fmt.Println(strings.Replace(dir, " ", "\\ ", -1))
}
UPDATE : This is similar to the Linux/Unix command pwd
-- return working directory name.
See also : Golang : get the current working directory of a running program
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
+5.1k Golang : Derive cryptographic key from passwords with Argon2
+28k Get client IP Address in Go
+16k Golang : Implement getters and setters
+15.8k Convert JSON to CSV in Golang
+5.7k Golang : Rot13 and Rot5 algorithms example
+32.1k Golang : Proper way to set function argument default value
+4.8k Javascript : Generate random key with specific length
+13.1k Golang : Basic authentication with .htpasswd file
+5.2k Golang : When to use make or new?
+10.8k Golang : Convert int(year) to time.Time type
+10.1k Golang : Change date format to yyyy-mm-dd