In this tutorial, We are going to learn about how to get the current working directory in go golang.
We can get current working directory using Getwd() function in os package in go golang.
Getwd() function returns a rooted path name corresponding to the current working directory.
If It is possible to reach current working directory via multiple paths (due to symbolic links) then Getwd() function may return any one of them.
Getwd() function prototype:
func Getwd() (dir string, err error)
Return Type:
- dir : Current working directory path
- err : It is type of error.
Example:
package main
import (
"fmt"
"os"
)
func main() {
// Get the current working directory
curr_wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Print the current working directory
fmt.Println(curr_wd)
}
Output:
/Users/John/go-workspace/src/main
To learn more about golang, You can refer given below link:
https://www.techieindoor.com/go-lang-tutorial/
References:
https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println