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
+8.7k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+7.5k Javascript : How to check a browser's Do Not Track status?
+16.5k Golang : Get input from keyboard
+5.7k Golang : Measure execution time for a function
+9.5k Golang : Get current, epoch time and display by year, month and day
+13.6k Golang : Fix image: unknown format error
+19.1k Golang : How to Set or Add Header http.ResponseWriter?
+5.1k Golang : If else example and common mistake
+4.3k Linux : sudo yum updates not working
+17.9k Golang : Get path name to current directory or folder
+20.7k Golang : Sort and reverse sort a slice of strings
+29k Golang : Login(Authenticate) with Facebook example