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
+45.7k Golang : Check if item is in slice/array
+2k Javascript : Put image into Chrome browser's console
+2.7k Golang : Struct field tags and what is their purpose?
+5.2k Golang : Print UTF-8 fonts on image example
+1.6k Golang : Calculate US Dollar Index (DXY)
+4.7k Golang : Forwarding a local port to a remote server example
+8.2k Golang : Read large file with bufio.Scanner cause token too long error
+6.5k Golang : Loop each day of the current month example
+12.3k Golang : Example for RSA package functions
+9.9k Golang : Padding data for encryption and un-padding data for decryption
+2.5k Golang *File points to a file or directory ?