【发布时间】:2020-01-01 09:35:28
【问题描述】:
golang 中的 os.Chdir() 无法正常工作。
package main
import (
"fmt"
"os"
)
func main() {
command := "cd C:\\"
if err := os.Chdir(command[3:]); err != nil {
fmt.Println("Error:\tCould not move into the directory (%s)\n")
}
}
输出:
Error: Could not move into the directory
我做错了什么或错过了什么?
【问题讨论】:
-
可能是权限问题
-
你能显示原来的错误信息吗?
-
您应该在
Println参数的末尾添加, err -
Chdir 将目录名称作为参数,而不是 shell 命令。错误会告诉你没有名为“cd C:\”的目录。
-
@Peter:不是这样的!代码
os.Chdir(command[3:]).