很简单的调用:
cmd := exec.Command( "TeraCopy.exe", fmt.Sprintf(`*"%s" "%s"`, copyPaths, targetDir) ) err := cmd.Start() 报错:
--------------------------- TeraCopy - Error --------------------------- File not found: \e:\**\tc.tmp \E:\**\b\ --------------------------- 确定 --------------------------- ??? 啥玩意 ???
本着出了问题先找自身原因的混帐话优良传统,控制台与 TeraCopy 软件试了各种参数,都正常……
感觉是转义出了问题,谷歌了半天,除了复制粘贴就没别的了。
米田共里淘金终于发现了一片文章:Go 在 windows 上调用本地进程传参时的一个天坑
MD ,最终 tm 还是 go 的问题,一直不敢往那想,属实被喷怕了。 摘抄一下:
On Windows, processes receive the whole command line as a single string and do their own parsing. Command combines and quotes Args into a command line string with an algorithm compatible with applications using CommandLineToArgvW (which is the most common way). Notable exceptions are msiexec.exe and cmd.exe (and thus, all batch files), which have a different unquoting algorithm. In these or other similar cases, you can do the quoting yourself and provide the full command line in SysProcAttr.CmdLine, leaving Args empty.
反正我看不懂,看人家的解释:
也就是说,针对 cmd 参数加的引号参数会有不同的逻辑,必须在 SysProcAttr.CmdLine 中写入原始参数了,但是 Args 留空,又会导致 SysProcAttr 值为 nil ,所以简单赋值也是不行的>
改了一下代码:
cmdLine := fmt.Sprintf(`copy *"%s" "%s"`, copyPaths, targetDir) cmd := exec.Command("TeraCopy.exe") cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/c " + cmdLine} err := cmd.Start() 解决。
学习 go 一段时间,觉得它的开发者很矛盾,比如三元运算符,很多人都想要它,但官方却以语法统一、可能会导致阅读困难之类的理由推脱。 但
return r ? true : false 不比你 if 要整洁易读? 对多字节的处理也很费劲,我到现在不知道怎样查找某个中文的位置,IndexRune 报错,也没谷歌到答案
fmt.Println(strings.IndexRune("学习", "习")) Error ./prog.go:9:42: cannot use "习" (untyped string constant) as rune value in argument to strings.IndexRune 是不是有人要说,爱用用,不用滚呢?
