gobuffalo / packr

The simple and easy way to embed static files into Go binaries.
MIT License
3.41k stars 196 forks source link

go generate? #48

Closed tj closed 6 years ago

tj commented 6 years ago

Looks like a nice replacement for go-bindata! But it seems to not allow you to use it with go:generate, unless that wasn't the intent. It'd be awesome to just drop // go:generate packr in there.

go-bindata had a few weird defaults so you always have to specify flags, I usually end up doing this if checked into source:

//go:generate go-bindata -ignore bindata.go -modtime 0 -pkg NAME .

or I guess just:

//go:generate go-bindata -ignore bindata.go -pkg NAME .

if it's not checked into GIT, but I think the CWD and name could be inferred easily and would be a nicer experience

cheers!

sagikazarmark commented 6 years ago

I believe this is already possible and can even work without the packr binary installed.

Here is a working example: https://github.com/twirphp/twirp/commit/4b6852129e505375ca96e090440fc1044fce15f1

It might be a good idea to document this, so packr can reach a higher score in this wonderful post:

http://tech.townsourced.com/post/embedding-static-files-in-go/

markbates commented 6 years ago
//go:generate packr2 -v

package main

import (
    "net/http"

    "github.com/gobuffalo/packr/v2"
)

func main() {
    box := packr.New("my box", "./templates")
    http.Handle("/", http.FileServer(box))

    http.ListenAndServe(":3000", nil)

}

This definitely works in v2. Closing this, please re-open if it's still an issue. Thanks.