The Wayback Machine - https://web.archive.org/web/20240225000703/https://www.geeksforgeeks.org/how-to-get-the-current-working-directory-in-golang/
Open In App
Related Articles

How to Get the Current Working Directory in Golang?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Getwd functionis used to get the current working directory in Golang, the function returns the rooted path name and if the current directory can be reached via multiple paths, the function can return any one of them. Syntax:
func Getwd()(dir string, err error)
The func returns two things the directory and also error, if there is no error it returns nil.
// Golang code for printing
// current working directory
  
package main
  
import (
    "fmt"
    "os"
)
  
func main() {
  
    // using the function
    mydir, err := os.Getwd()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(mydir)
}

                    
Output:
users/home/desktop
You may get different outputs on online compilers. For better understanding, use the offline compiler.

Last Updated : 10 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads