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
+6.5k Mac/Linux/Windows : Get CPU information from command line
+7.9k Golang : Check if integer is power of four example
+12k Golang : Extract part of string with regular expression
+19.4k Golang : Append content to a file
+11.8k Golang : Get month name from date example
+17.4k Golang : Login and logout a user after password verification and redirect example
+5.8k Golang : Dealing with backquote
+20.8k Golang : Convert(cast) string to rune and back to string example
+16.6k Golang : Find file size(disk usage) with filepath.Walk
+7.7k Android Studio : Rating bar example
+11.4k Golang : convert(cast) float to string
+4.7k Golang : Check if a word is countable or not