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
+28.4k Golang : How to verify uploaded file is image or allowed file types
+11.7k Golang : http.Get example
+9k CodeIgniter : Load different view for mobile devices
+6k Golang : Fibonacci number generator examples
+7.8k Golang : Find duplicate files with filepath.Walk
+21.3k Golang : Round float to precision example
+4.2k Golang : Pad file extension automagically
+5.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+35.3k Golang : Convert(cast) int64 to string
+9.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+8.9k Golang : Setting variable value with ldflags
+18.3k Golang : Count JSON objects and convert to slice/array