douke1942 2016-12-22 07:01
浏览 258
已采纳

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

What is the best way to map a network share to a windows drive using go-lang? This share also requires a username and password. A similar question was asked for python What is the best way to map windows drives using Python?

  • 写回答

1条回答 默认 最新

  • dongnong3799 2017-06-20 08:54
    关注

    As of now there is no direct way to do that in Go; I would recommend using net use, which of course limits the functionality to Windows, but that's actually what you need.

    So, when you open a command prompt in Windows you can map network shares to Windows drives by using:

    net use Q: \\SERVER\SHARE /user:Alice pa$$word /P

    Q: represents your windows drive, \\SERVER\SHARE is the network address, /user:Alice pa$$word are your credentials, and /P is for persistence.

    Executing this in Go would look something like:

    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))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路