How can I open files relative to my GOPATH?

前端 未结 4 792
忘掉有多难
忘掉有多难 2020-12-05 09:36

I\'m using io/ioutil to read a small text file:

fileBytes, err := ioutil.ReadFile(\"/absolute/path/to/file.txt\")

And that wor

4条回答
  •  难免孤独
    2020-12-05 09:48

    I think Alec Thomas has provided The Answer, but in my experience it isn't foolproof. One problem I had with compiling resources into the binary is that compiling may require a lot of memory depending on the size of your assets. If they're small, then it's probably nothing to worry about. In my particular scenario, a 1MB font file was causing compilation to require somewhere around 1GB of memory to compile. It was a problem because I wanted it to be go gettable on a Raspberry Pi. This was with Go 1.0; things may have improved in Go 1.1.

    So in that particular case, I opt to just use the go/build package to find the source directory of the program based on the import path. Of course, this requires that your targets have a GOPATH set up and that the source is available. So it isn't an ideal solution in all cases.

提交回复
热议问题