golang:在Windows上运行pdf文件的默认应用程序 [英] golang: run default application for a pdf file on windows

查看:128
本文介绍了golang:在Windows上运行pdf文件的默认应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在默认应用程序中打开文件系统中的PDF文件。我怎样才能做到这一点?从命令行我只写了PDF文件的文件名,应用程序打开(与请求的文件)。当我尝试使用 exec.Command()时,我得到一个错误(不奇怪) exec:foo.pdf:找不到的可执行文件%PATH%

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

}


推荐答案

您必须启动 cmd / C start foo.pdf 。这会让启动命令找到正确的可执行文件。

You must launch cmd /C start foo.pdf. This will let the start command find the correct executable for you.

cmd := exec.Command("cmd", "/C start path_to_foo.pdf")

这篇关于golang:在Windows上运行pdf文件的默认应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆