How to change the current working directory using the os package in Golang?

In Go, you can use the os package to change the current working directory by using the Chdir function. Here's an example:

package main import ( "fmt" "os" ) func main() { // Get the current working directory cwd, _ := os.Getwd() fmt.Println("Current working directory:", cwd) // Change the current working directory err := os.Chdir("/path/to/new/directory") if err != nil { fmt.Println("Error:", err) return } // Get the updated working directory cwd, _ = os.Getwd() fmt.Println("Updated working directory:", cwd) }

Replace /path/to/new/directory with the path to the directory you want to set as the new working directory. The os.Chdir function returns an error if the directory change operation fails, so make sure to handle the error appropriately.