How to access resource files after the 'go' tool installed the executable?

后端 未结 3 1156
情书的邮戳
情书的邮戳 2020-12-18 03:07

Lets assume that running go install -v importpath builds an executable and installs it into $GOPATH/bin/program. The go tool is unable

相关标签:
3条回答
  • 2020-12-18 03:42

    GOPATH environment variable

    On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.

    Therefore, $GOPATH/bin/program and $GOPATH/src/importpath are not necessarily valid. For example, on Linux,

    $ GOPATH=$HOME/gopath:$HOME/go
    $ cd $GOPATH/bin/go
    bash: cd: /home/peter/gopath:/home/peter/go/bin/go: No such file or directory
    
    0 讨论(0)
  • 2020-12-18 03:46

    go install is not designed to do what you want to do.

    If you insist on using go install, you can embed the resources in the executable (byte arrays in source code).

    Another option is to use a deploy script that runs go install and then copies the resources to a place known by your executable.

    If you want your program to be installable by people other than you, you should use a packaging system that is standard on the os you're targeting (e.g. apt/rpm on Linux, an installer executable on Windows, .dmg file on Mac etc.)

    0 讨论(0)
  • 2020-12-18 03:53

    This question has been asked multiple times on the go-nuts mailing list. The go tool doesn't offer a direct way to distribute additional resources at the moment. However, there are two workarounds available:

    • write a simple script (awk should be sufficient) to convert any file into a .go file which just contains a single string constant and embed the file directly into the binary. This approach is used by the Camlistore project for example (camlistored/ui/fileembed.go). go-bindata does similar things.

    • go-tour and many other projects are currently using the Import function of the go/build package to search through all src/ folders listed in $GOPATH and $GOROOT to find the correct path of the package sources. Example: http://code.google.com/p/go-tour/source/browse/gotour/local.go?r=996704f8ef9e63949b3bc4d94b613ec4a7b5d99a#53

    0 讨论(0)
提交回复
热议问题