How to get the current user's home directory path using the os package in Golang?

To get the current user's home directory path using the os package in Go, you can make use of the os.UserHomeDir function. Here's an example:

package main import ( "fmt" "os" ) func main() { homeDir, err := os.UserHomeDir() if err != nil { fmt.Println("Failed to get home directory:", err) return } fmt.Println("Home directory:", homeDir) }

In this example, we first import the necessary packages fmt and os. Then, we use the os.UserHomeDir function to get the home directory path of the current user. If an error occurs, we print an error message and return. Otherwise, we print the home directory path.