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
+25.7k Golang : Find files by name - cross platform example
+8.4k Golang : Changing a RGBA image number of channels with OpenCV
+17.4k Golang : Get path name to current directory or folder
+27.8k Golang : Get first few and last few characters from string
+6.8k Golang : Error reading timestamp with GORM or SQL driver
+33.8k Golang : Strip slashes from string example
+11.5k Golang : HTTP response JSON encoded data
+6.9k Golang Hello World Example
+5.1k Golang : How to verify input is rune?
+7.3k Useful methods to access blocked websites
+16.3k Golang : Linked list example
+11.8k Golang : Convert(cast) uintptr to string example