markbates / pkger

Embed static files in Go binaries (replacement for gobuffalo/packr)
MIT License
1.19k stars 60 forks source link

strange behavior on pkger.Open api #134

Closed dfang closed 4 years ago

dfang commented 4 years ago

it seems pkger.Open() can't pass string variable

it takes me a while to debug template not load issue in my issue, so i started it with simple.

steps to reproduce:

https://github.com/markbates/pkger/tree/master/examples/open/pkger

change run method in main.go

before: 
func run() error {
    f, err := pkger.Open("/public/index.html")
        ....
}

after:
func run() error {
    t := "/public/index.html"
    fmt.Println(t)
    f, err := pkger.Open(t)
        ....
}
$ pkger
$ go build -v -o example
$ ./example
/public/index.html
2020/11/24 18:04:50 file does not exist
phanirithvij commented 4 years ago

pkger cannot parse string variables because if it should, it needs to run your code when pkging to determine what the sting variable t contains. But whereas if you have pkger.Open("/public/index.html") i.e. defined as a string, it needs to just parse your code and not run it to know that it needs to embed /public/index.html.

So you can either use the before way you mentioned or before you open any file or directory include it

pkger.Include("/public/index.html")
dfang commented 4 years ago

make sense