使用 golang 映射 windows 驱动器的最佳方法是什么?

What is the best way to map windows drives using golang?

使用 go-lang 将网络共享映射到 windows 驱动器的最佳方法是什么?此共享还需要用户名和密码。 python What is the best way to map windows drives using Python?

也有人问过类似的问题

到目前为止,在 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))
}