How to Get the Current Working Directory in Golang?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


Output:

users/home/desktop

You may get different outputs on online compilers. For better understanding, use the offline compiler.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.