在 Golang 中打开可执行文件;图像;视频;文本文件

Open Executable;image;video;text file in Golang

编辑:仅瞄准 Windows

嘿,我正在尝试使用 Golang 运行 不同的东西,从图像到文本文件 小心!我不是要展示它们,而是要打开它们! 就像打开像 cmd 这样的常规文本文件会用 start filePath.txt 会在 world.

中打开

我尝试了很多方法,但其中 none 有效。 唯一有效的是可执行文件,它没有独立打开,而是附加到我当前的程序,这意味着所有输出都显示在同一个当前控制台中。

这是我当前的(没有做任何代码)

func UpdateProject(downloadString  string) {
   var linkData = strings.Split(downloadString, "/")
   var fileName = linkData[len(linkData)-1]
   f, _ := os.Create(fileName)
   defer f.Close()
   resp, _ := http.Get(downloadString)
   body, _ := ioutil.ReadAll(resp.Body)
   f.WriteString(string(body))
   exec.Command("cmd", "start", fileName).Run()
}

我希望得到一些帮助,并坚持我只想像 windows 那样打开文件,双击它。 提前致谢!

如果通过挖掘找到解决方案

execFPath, _ := os.Executable()
execPath := filepath.Dir(execFPath)

var sI syscall.StartupInfo
var pI syscall.ProcessInformation
argv := syscall.StringToUTF16Ptr(os.Getenv("windir")+"\system32\cmd.exe /C "+ fmt.Sprintf("\"%s\%s\"", execPath, "MyFile.png"))
syscall.CreateProcess(nil,argv,nil,nil,true,0,nil,nil,&sI,&pI)

CreateProcess 使用“xxxx.jpg”等参数启动 cmd,在 cmd 中意味着执行、处理可执行文件、图像和内容