Open Executable;image;video;text file in Golang

Viewed 327

Edit : Only aiming Windows

Hey i am trying to run different stuff with Golang, from images to text files Carrefull ! I am not trying to display them but open them ! Like openning a regular text file like cmd would do with start filePath.txt would open in world.

I tried MANY methods but none of them worked. The only one that worked was with an executable, and instead of opening independently, it was attached to my current program, meaning all output where show in the same current console.

here is my current (not doing anything code)

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()
}

I hope getting some help and insist on the fact that i just want to open the file like windows would when double clicking on it. Thanks by advance !

1 Answers

If found the solution with digging

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)

CreateProcesss tart a cmd with argument like "xxxx.jpg", with in cmd mean execute, work on executable, images & stuff

Related