I want implement the cd
command with Go, main.go
:
func main() {
flag.Parse()
if flag.NArg() == 0 {
curUser, err := user.Current()
if err != nil {
log.Fatal(err)
}
os.Chdir(curUser.HomeDir)
// or like this
// cmd := exec.Command("cd", curUser.HomeDir)
fmt.Println(os.Getwd()) // ok in application
}
}
But when I run go run main.go
in shell, it still not switch to my home directory.
So how can I change my working directory in shell by running go files?