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 /PQ:represents your windows drive,\\SERVER\SHAREis the network address,/user:Alice pa$$wordare your credentials, and/Pis 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)) }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报