Attach files to compiled go program

1,019 views
Skip to first unread message

Konstantin Kanellopoulos

unread,
Apr 1, 2014, 7:08:23 PM4/1/14
to golan...@googlegroups.com
Hi,

I spend some evenings to this idea and would appriciate your feedback.

I'm a fairly fresh gopher, so please me tell if I get something fundamentally wrong here.

The Idea


I really like having a single executable after compiling my go code.
This stops with multiple non-go files which your app relies on.
i.e. Templates, Images, Javascript, CSS, etc ...

I want to append all files as tar(.gz?) to my executable and make them
available for the application at runtime.

I know there's allready an other solution which converts files into go code
but I'd like to be able to change the appended data without recompiling and
maybe also compress them.


The API

Currently I try to make the following work:

package main

import(
    "fmt"
    "io/ioutil"
   
    // I currently called it bpack which is a abbrevation for backpack
    "bpack"
)

func main() {
    // The backpack returns io.Reader for the requested content
    r, err := bpack.Get("/public/js/jquer.min.js")
    if err != nil {
        // handle
    }
   
    content, err:= ioutil.ReadAll(r)
    if err != nil {
        // handle
    }
   
    fmt.Println("%s\n", content)
}

If someone want's to join me, I will put the code on github.

Thanks,
Konsi

Rodrigo Kochenburger

unread,
Apr 1, 2014, 7:10:14 PM4/1/14
to Konstantin Kanellopoulos, golan...@googlegroups.com
There is a couple of other implementations of this already. For example: https://github.com/GeertJohan/go.ricec

- RK


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hotei

unread,
Apr 1, 2014, 10:15:43 PM4/1/14
to golan...@googlegroups.com
I think the Rodrigo's post should have ended with "go.rice" 

Ondekoza

unread,
Apr 1, 2014, 10:34:53 PM4/1/14
to golan...@googlegroups.com


On Tuesday, April 1, 2014 7:08:23 PM UTC-4, Konstantin Kanellopoulos wrote:

Konstantin Kanellopoulos

unread,
Apr 2, 2014, 3:05:12 AM4/2/14
to golan...@googlegroups.com
I know there's allready an other solution which converts files into go code
but I'd like to be able to change the appended data without recompiling and
maybe also compress them.
 
Thank you. I allready knew about that ;-)

Konstantin Kanellopoulos

unread,
Apr 2, 2014, 3:19:37 AM4/2/14
to golan...@googlegroups.com, Konstantin Kanellopoulos
Hi,

thank you for your link. I looked into it, but still feel that is should roll
my own implementation. (at least for sake of learninig go)

Rice uses "Boxes" to organize the files. It feels a little bit overengineered to me.
I*d prever something more simple. Retrieve a file by a path. If its not in the backpack, load it from filesystem.
Message has been deleted

Tamás Gulácsi

unread,
Apr 2, 2014, 3:29:44 AM4/2/14
to golan...@googlegroups.com
It isn't that complicated: mentally replace "Box" with "SubDir".
Please try box, and help develop it! We already have at least 5 such half-finished projects!

Dobrosław Żybort

unread,
Apr 2, 2014, 4:03:26 AM4/2/14
to golan...@googlegroups.com, Konstantin Kanellopoulos
> I*d prever something more simple. Retrieve a file by a path. If its not in the backpack, load it from filesystem.
That's exactly what go.rice is doing:
https://github.com/GeertJohan/go.rice#gorice

David Skinner

unread,
Apr 2, 2014, 9:06:20 PM4/2/14
to golan...@googlegroups.com
I am using http://godoc.org/gopkg.in/cookieo9/resources-go.v2 and have had no problems so far.

Miki Tebeka

unread,
Apr 3, 2014, 2:23:01 AM4/3/14
to golan...@googlegroups.com
There's also nrsc ;)

Konstantin Kanellopoulos

unread,
Apr 3, 2014, 3:26:42 AM4/3/14
to golan...@googlegroups.com
Am Donnerstag, 3. April 2014 03:06:20 UTC+2 schrieb David Skinner:
I am using http://godoc.org/gopkg.in/cookieo9/resources-go.v2 and have had no problems so far.

That's exactly what I wanted. Thank you ;-)

Geert-Johan Riemer

unread,
Apr 3, 2014, 5:17:23 AM4/3/14
to golan...@googlegroups.com, Konstantin Kanellopoulos
I like your idea of tar.gz, I don't know why I didn't think of that..
The current append feature in rice requires zip to be installed, which causes problems on windows.
I wonder if tar.gz can do the same as zip, without the need for an external command.

There's a TODO to add "Single's" (single file), beside Boxes (folders).
The complete file virtualization can stay the same, but it needs implementation in the tool and go.rice package to load the 'Single' run-time.

Currently, rice allows for "embedding" (generating go source containing the resources) and "appending" (adding zip to exec).
I'm thinking about adding feature to generate object files that the linker will add to the executable.
The whole idea of go.rice is that resources can be added to an executable in several ways, without having to change the code depending on those resources.

/GeertJohan

Tamás Gulácsi

unread,
Apr 3, 2014, 6:31:32 AM4/3/14
to golan...@googlegroups.com
Zip: the file listing is at the file's end, thus you can append a zip to anything and after fixing the offsets, the file can be read from the beginning as usual, or as a zip (from the end).

Tar is a streaming format, thus you need to read the whole to know what is in there. But if you invent your own zip format, then it is possible.

Jens Alfke

unread,
Apr 3, 2014, 10:54:35 AM4/3/14
to golan...@googlegroups.com


On Wednesday, April 2, 2014 6:06:20 PM UTC-7, David Skinner wrote:
I am using http://godoc.org/gopkg.in/cookieo9/resources-go.v2 and have had no problems so far.

This looks useful, but the docs are somewhat vague — it says resources can be loaded from "a zip file embedded in the executable", which is exactly what I'd want, but I couldn't find anything explaining how you embed a zip file in a Go executable. Can someone explain?

—Jens

Tamás Gulácsi

unread,
Apr 3, 2014, 11:30:48 AM4/3/14
to golan...@googlegroups.com
Go build && cat exe zip >exe2 && zip -A exe2

Jens Alfke

unread,
Apr 3, 2014, 11:40:44 AM4/3/14
to Tamás Gulácsi, golan...@googlegroups.com

On Apr 3, 2014, at 8:30 AM, Tamás Gulácsi <tgula...@gmail.com> wrote:

Go build && cat exe zip >exe2 && zip -A exe2

OK, this appends the Zip archive onto the binary. Does this work on all architectures? I.e. do the loaders for Mach-O, ELF, Windows, etc. all ignore extra data past the end of the binary?

—Jens

Tamás Gulácsi

unread,
Apr 3, 2014, 12:20:13 PM4/3/14
to golan...@googlegroups.com
AFAIK all starts reading from the beginning and no thing points after the executables end.

David Skinner

unread,
Apr 3, 2014, 2:42:07 PM4/3/14
to Tamás Gulácsi, golan...@googlegroups.com
The readme file on github gave explicit insturctions. https://github.com/cookieo9/resources-go/tree/v2

I am currently only using Debian sid AMD64. Cannot vouch for other cases.


On Thu, Apr 3, 2014 at 11:20 AM, Tamás Gulácsi <tgula...@gmail.com> wrote:
AFAIK all starts reading from the beginning and no thing points after the executables end.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/_LszV5IqBWg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

Gyepi SAM

unread,
Apr 3, 2014, 3:20:12 PM4/3/14
to Jens Alfke, Tamás Gulácsi, golan...@googlegroups.com
I am most familiar with the ELF format, but those are all structured files and
have a length value encoded in there somewhere so yes, the loaders should ignore
the extra data.

In any case, that should be easy enough to verify with a small program and a
test zip.

Also, note that the Go stdlibs contain readers for all three formats if you
need to manipulate the files or just read the docs.

-Gyepi

Reply all
Reply to author
Forward
0 new messages