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
+19.9k Golang : How to get time from unix nano example
+6.3k Golang : Test input string for unicode example
+24k Golang : Upload to S3 with official aws-sdk-go package
+39k Golang : How to iterate over a []string(array)
+13.9k Golang : Get current time
+24.4k Golang : GORM read from database example
+4.3k Golang : Valued expressions and functions example
+7.2k Golang : File system scanning
+3.7k Golang : Switch Redis database redis.NewClient
+12.7k Golang : zlib compress file example
+16.4k Golang : Check if a string contains multiple sub-strings in []string?