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
+4.8k PHP : Get client IP address
+11.3k Golang : Sort and reverse sort a slice of bytes
+8.6k Golang : Turn string or text file into slice example
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+40.8k Golang : Get hardware information such as disk, memory and CPU usage
+4.9k Golang : Find change in a combination of coins example
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+10.8k Golang : Simple client-server HMAC authentication without SSL example
+7.9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+6.3k Golang : Check to see if *File is a file or directory
+12.1k Golang : Generate Code128 barcode
+12.8k Golang : Chunk split or divide a string into smaller chunk example