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
+32.2k Golang : All update packages with go get command
+4.4k Golang : fmt.Println prints out empty data from struct
+28.2k Golang : How to verify uploaded file is image or allowed file types
+4.5k Golang : Stop goroutine without channel
+13.3k Golang : Get URI segments by number and assign as variable example
+35.8k Golang : Read a text file and replace certain words
+13.9k Golang : rune literal not terminated error
+29.3k Golang : bufio.NewReader.ReadLine to read file line by line
+10.3k Golang : Generate DSA private, public key and PEM files example
+19.2k Golang : Convert date string to variants of time.Time type examples
+8.2k Golang : Qt Yes No and Quit message box example
+17.9k Golang : Execute shell command