use the os package to get the current working directory in go

To get the current working directory in Go using the os package, you can use the Getwd() function. This function returns a string with the path of the current working directory.

Here's an example code snippet:

main.go
package main

import (
	"fmt"
    "os"
)

func main() {
	wd, err := os.Getwd()
	if err != nil {
		fmt.Println("Error:", err)
	}
	fmt.Println("Current working directory:", wd)
}
177 chars
15 lines

In this code, we import the os package and use the Getwd() function to get the current working directory. This function returns two values: the string with the working directory path and an error. We check the error in the if statement and print the current working directory using the fmt.Println() function.

gistlibby LogSnag