到目前为止,在 Go 中还没有直接的方法可以做到这一点;我建议使用net use,这当然会将功能限制在 Windows 中,但这实际上是您所需要的。
因此,当您在 Windows 中打开命令提示符时,您可以使用以下方法将网络共享映射到 Windows 驱动器:
net use Q: \\SERVER\SHARE /user:Alice pa$$word /P
Q: 代表您的 Windows 驱动器,\\SERVER\SHARE 是网络地址,/user:Alice pa$$word 是您的凭据,/P 是持久性。
在 Go 中执行这个看起来像:
func mapDrive(letter string, address string, user string, pw string) ([]byte, error) {
// return combined output for std and err
return exec.Command("net use", letter, address, fmt.Sprintf("/user:%s", user), pw, "/P").CombinedOutput()
}
func main() {
out, err := mapDrive("Q:", `\\SERVER\SHARE`, "Alice", "pa$$word")
if err != nil {
log.Fatal(err)
}
// print whatever comes out
log.Println(string(out))
}