Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang: run default application for a pdf file on windows

Tags:

windows

go

I'd like to open a PDF file in the filesystem from go with the default application. How can I do that? From the command line I just write the filename of the pdf file and the application opens (with the requested file). When I try to use exec.Command() I get an error (not surprisingly) exec: "foo.pdf": executable file not found in %PATH%.

package main

import (
    "log"
    "os/exec"
)

func main() {
    cmd := exec.Command("foo.pdf")
    err := cmd.Start()
    if err != nil {
        log.Fatal(err)
    }
    err = cmd.Wait()
    if err != nil {
        log.Fatal(err)
    }

}
like image 672
topskip Avatar asked Aug 22 '12 12:08

topskip


1 Answers

exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", "path_to_foo.pdf")

should also handle it.

Note that still the right way to do it is to use a C wrapper around the ShellExecute() API function exported by shell32.dll, and the "w32" library seems to provide this wrapper right away.

like image 179
kostix Avatar answered Oct 20 '22 01:10

kostix