Resource bundling

2,478 views
Skip to first unread message

Daniel Connelly

unread,
Feb 27, 2012, 7:25:44 PM2/27/12
to golang-nuts
Hi all,

Let's say I want to build a standalone application that needs some
resources, like template files or images, bundled with it. In Java,
you can create JAR files that bundle together libraries and resources
in one executable. Is there a best-practice for doing something like
this in Go?

Thanks,
Daniel Connelly

Kyle Lemons

unread,
Feb 27, 2012, 7:34:26 PM2/27/12
to Daniel Connelly, golang-nuts
It's a common enough practice to embed them as constants in your application.  As for shared libraries, go is itself statically linked (but you may depend on some C libraries depending on various factors), so you don't normally need to bundle those.  Files that need to be accessed during runtime should be transmitted along with the executable however you're going to do that.  As for having some sort of executable go archive format, I don't think I've seen anyone post such a solution, but it wouldn't be that difficult to add.  What might be a more fun alternative would be to embed them (compressed, probably) in extra sections of the binary, and provide a os.File-like API to access them.

AllenDang

unread,
Feb 27, 2012, 8:53:12 PM2/27/12
to golang-nuts
On Windows, you can compile and embed a resource (*.rc) file into exe
like this:

windres resource.rc -o temp-rc.o
go tool 8g -I %GOPATH%\pkg\windows_386 app.go
go tool pack grc _go_.8 app.8 temp-rc.o
go tool 8l -L %GOPATH%\pkg\windows_386 -o app.exe -s -Hwindowsgui _go_.
8

Daniel Connelly

unread,
Feb 27, 2012, 9:18:53 PM2/27/12
to AllenDang, golang-nuts
Interesting--how do you access the resource file from the program?
(Is this peculiar to Windows? I've never encountered .rc files)

--
Daniel Connelly
dhconnelly.com

Guillaume Lescure

unread,
Feb 27, 2012, 9:48:31 PM2/27/12
to golan...@googlegroups.com, AllenDang
You have the same feature in Qt with .qrc and that's cross-platform ... so it's far better but both cases can be use in a particlar contexte.
For example, in Qt, every ressources have a path like : ":/my_ressource.png" or ":/example_folder/my_style_sheet.css". But it only can be understood by Qt.

I think it's very difficult (maybe impossible) to use it with Go.
Maybe, in a futur Go version, there will be a feature like that for Go but not now.




Le lundi 27 février 2012 21:18:53 UTC-5, dhconnelly a écrit :
Interesting--how do you access the resource file from the program?
(Is this peculiar to Windows? I've never encountered .rc files)  

--

Daniel Connelly
dhconnelly.com 

AllenDang

unread,
Feb 27, 2012, 10:22:13 PM2/27/12
to golang-nuts
This is peculiar to Windows. So there are bunch of win32 apis I can
use to access the resource.

Brad Fitzpatrick

unread,
Feb 27, 2012, 10:59:18 PM2/27/12
to Daniel Connelly, golang-nuts
Here's how Camlistore does it:

You define a file with a magic comment saying which files (a regexp) you want to embed:

... that file also has to depend on "camlistore.org/pkg/fileembed" and have a Files variable:

var Files = &fileembed.Files{
   OverrideEnv: "CAMLI_DEV_UI_FILES",
}

The OverrideEnv is optional and say that if that environment variable is present, then the embedded resources are only a fallback, but the files on disk in that location take priority.  If that environment variable isn't present, the disk is never touched.  (this is useful for quick iteration during development).

Then you run the genfileembeds command to generate zembed_*.go files:


Which makes all the files you see here:


Then, to use this, it's just like:

import (
          "net/http"
          ...
          "camlistore.org/server/camlistored/ui"
)
...
        http.FileServer(ui.Files)

(or whatever)

Brad Fitzpatrick

unread,
Feb 27, 2012, 11:01:38 PM2/27/12
to Daniel Connelly, golang-nuts
Oh, also:  these tools & libraries should be accessible with "go get" soon, pending:

http://code.google.com/p/go/issues/detail?id=3099 / http://codereview.appspot.com/5660051/

$ editor /path/to/your/resources/fileembed.go
$ genfileembed ./path/to/your/resources/

Daniel Connelly

unread,
Feb 27, 2012, 11:08:56 PM2/27/12
to Brad Fitzpatrick, golang-nuts
This is perfect. Thanks!

unread,
Feb 28, 2012, 4:02:26 AM2/28/12
to golang-nuts

OmarShariffDontLikeIt

unread,
Feb 28, 2012, 3:21:23 AM2/28/12
to golang-nuts
Personally I use go-bindata:
https://github.com/jteeuwen/go-bindata

I use this for images, html templates, javascript etc, and it ensures
that I have all the resources available to my executable. I hook it up
using a make file to generate the Go sourcecode.

On Feb 28, 4:08 am, Daniel Connelly <dhconne...@gmail.com> wrote:
> This is perfect.  Thanks!
>
>
>
>
>
>
>
> On Mon, Feb 27, 2012 at 11:01 PM, Brad Fitzpatrick <bradf...@golang.org> wrote:
> > Oh, also:  these tools & libraries should be accessible with "go get" soon,
> > pending:
>
> >http://code.google.com/p/go/issues/detail?id=3099 / http://codereview.appspot.com/5660051/
>
> > Then you can do:
>
> > $ go get camlistore.org/pkg/fileembed/genfileembed
> > $ editor /path/to/your/resources/fileembed.go
> > $ genfileembed ./path/to/your/resources/
>
> > On Tue, Feb 28, 2012 at 2:59 PM, Brad Fitzpatrick <bradf...@golang.org>
> > wrote:
>
> >> Here's how Camlistore does it:
>
> >> You define a file with a magic comment saying which files (a regexp) you
> >> want to embed:
>
> >>http://code.google.com/p/camlistore/source/browse/server/camlistored/...
>
> >> ... that file also has to depend on "camlistore.org/pkg/fileembed" and
> >> have a Files variable:
>
> >> var Files = &fileembed.Files{
> >>    OverrideEnv: "CAMLI_DEV_UI_FILES",
> >> }
>
> >> The OverrideEnv is optional and say that if that environment variable is
> >> present, then the embedded resources are only a fallback, but the files on
> >> disk in that location take priority.  If that environment variable isn't
> >> present, the disk is never touched.  (this is useful for quick iteration
> >> during development).
>
> >> Then you run the genfileembeds command to generate zembed_*.go files:
>
> >>http://code.google.com/p/camlistore/source/browse/#git%2Fpkg%2Ffileem...
>
> >> Which makes all the files you see here:
>
> >>http://code.google.com/p/camlistore/source/browse/#git%2Fserver%2Fcam...
>
> >> Then, to use this, it's just like:
>
> >> import (
> >>           "net/http"
> >>           ...
> >>           "camlistore.org/server/camlistored/ui"
> >> )
> >> ...
> >>         http.FileServer(ui.Files)
>
> >> (or whatever)
>
> >> On Tue, Feb 28, 2012 at 11:25 AM, Daniel Connelly <dhconne...@gmail.com>

Dave Cheney

unread,
Feb 28, 2012, 7:04:06 AM2/28/12
to OmarShariffDontLikeIt, golang-nuts
That's a great package, thanks or sharing.

unread,
Mar 23, 2012, 10:22:58 AM3/23/12
to golan...@googlegroups.com

ch3c...@gmail.com

unread,
Jan 16, 2016, 11:13:32 AM1/16/16
to golang-nuts
What about go 1.5? The compiler architecture changed.
Does the same method work?

⚛於 2012年3月23日星期五 UTC+8下午10時22分58秒寫道:
Reply all
Reply to author
Forward
0 new messages