use the os package to get the current working directory and print it with the fmt package in go

To get the current working directory using the os package and print it using the fmt package in Go, you can use the following code:

main.go
package main

import (
    "fmt"
    "os"
)

func main() {
    dir, err := os.Getwd()
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Current working directory:", dir)
}
215 chars
16 lines

In this code, we first import the required packages os and fmt. We then use the Getwd() function from the os package to get the current working directory. If there is an error, we log it using the Println() function from the fmt package. Otherwise, we print the current working directory using Println().

gistlibby LogSnag