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

标签 windows go mapping drive

使用 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))
}

关于windows - 使用 golang 映射 Windows 驱动器的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277465/

相关文章:

ruby-on-rails - 怎么看Rails的开发日志呢?

javascript - Array.prototype.map() 之后的下一个函数可以安全地假设映射中的所有回调都已返回吗?

postgresql - 如何在 GORM 中将 PostgreSQL 函数设置为默认值?

http - 带嵌套参数的 POST 请求

entity-framework - 自引用实体不可为空

php - 关于根据 IP 地址[字面意思]映射位置的问题

xml - 任务计划程序 - 自定义事件过滤器触发器未触发操作

windows - haskell:输出非ASCII字符

windows - COMAdmin.COMAdminCatalog - 如何获取 DCOM Config 文件夹内容?

json - 如何使用Golang进行通用解码