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
+13.6k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+7.5k Golang : Another camera capture GUI application with GTK and OpenCV
+12.7k Golang : How to convert a number to words
+11.8k Golang : Convert IPv4 address to packed 32-bit binary format
+7.1k Golang : Find relative luminance or color brightness
+9.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+6.4k Golang : Lock executable to a specific machine with unique hash of the machine
+17.3k Golang : Write file with io.WriteString
+16.5k Golang : Simple client server example
+20.6k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+5.3k Golang : Test input string for unicode example
+8.1k Golang : Timeout example