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
+16.5k Golang : Put UTF8 text on OpenCV video capture image frame
+7.2k Golang : How To Use Panic and Recover
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+4.5k Golang : Intercept, inject and replay HTTP traffics from web server
+17k Golang : Iterating Elements Over A List
+9.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+7.7k Golang : Gonum standard normal random numbers example
+36.5k Golang : Comparing date or timestamp
+3.7k MariaDB/MySQL : How to get version information
+5.5k Nginx : Password protect a directory/folder
+22.8k Golang : How to validate URL the right way
+20.6k Golang : How to reverse slice or array elements order