Ship additional files with goinstall

275 views
Skip to first unread message

r2p2

unread,
Jan 1, 2012, 1:41:40 PM1/1/12
to golan...@googlegroups.com
I searched now for two hours to feel comfortable to ask the following questions.

Against the language specification/documentation is nothing to say. But it is hard to find information about the correct build and install process. The documentation for the goinstall tool tells us that it can install packages from github and so on. But thats all. What if I have a web application with external html/template files. I saw that those files are copied into the installed package place under $GOROOT. But how can I load them from inside the application. While development I don't know from which repository
this will be installed. I mean I can't hard code a path like$GOROOT/src/pkg/github.com/someusername/$PROJECT.

I think that it would be useful to have few more blogs and more extensive tutorials. I can't find useful references for more than the language specification.

Does that sound like self-pity?

Brian Ketelsen

unread,
Jan 1, 2012, 1:47:20 PM1/1/12
to golan...@googlegroups.com

if you want to bundle html/templates you need to include them as constant strings inside your source code:

const tmpl = `<html....`

Then they'll be inside your executable/package always and available when goinstall'd

Brian

John Asmuth

unread,
Jan 1, 2012, 1:57:42 PM1/1/12
to golan...@googlegroups.com
goinstall is not a magic bullet - it's just a tool that makes certain kinds of projects much easier to distribute.

For more complex stuff, don't be afraid to break out the makefiles!

Andrew Gerrand

unread,
Jan 3, 2012, 9:57:23 PM1/3/12
to golan...@googlegroups.com
The Camlistore project has an automated tool for packaging static text into .go source files, the relevant package is named fileembed: 


You can see it in use here:


Andrew

André Moraes

unread,
Jan 4, 2012, 11:07:48 AM1/4/12
to golan...@googlegroups.com
> this will be installed. I mean I can't hard code a path
> like$GOROOT/src/pkg/github.com/someusername/$PROJECT.

Hard coding paths isn't the best way to handle template files in a web
application.

You could write your app in a way that the function called to parse
the templates files take a parameter with the base path of your
template directories. Then in you app you declare a flag and use that
to discover the path (providing a good default).

This can be used for static files, etc...

If you will hard code the path, probably you should use the
alternative pointed by Andrew.


--
André Moraes
http://andredevchannel.blogspot.com/

Robert Johnstone

unread,
Jan 4, 2012, 3:47:48 PM1/4/12
to golang-nuts
Hello,

Because Go generates statically linked executables, I avoid packaging
additional files. Instead, encode the data as a byte array or string,
which is then compiled and included in the final application. Also,
bytes.Buffer is your friend for turning byte arrays and strings into
an interface useable instead of files in most cases.

If you're interested, I would suggest looking at this utility:
https://bitbucket.org/rj/fileembed-go/. I've only needed it for a
single project, so I have not taken the time to complete the
documentation or testing, but will do so if requested. The utility
work very well with Makefiles, as you can add the targets so that data
files are automatically encoded into a go source file, which can then
be complied along with the other files in your package or command.

Good luck,

Robert

r2p2

unread,
Jan 4, 2012, 4:52:25 PM1/4/12
to golan...@googlegroups.com
I don't know if I can get used to the idea of editing html inside go. Could become a bit obscure in bigger projects.
The automated transfer from file to a go code file is a cool, how is it called?, middleway? Inspired by the ideas I am thinking of an virtual filesystem (with directories) which is packed on compile time into code. The application could access those files nearly transparent by vfs.open("/html/index.html").

In case this is done in some of the links already, I have to apologize. Have at the moment not so much time to watch them in detail. Will do this at the weekend.

Thanks for all hints so far.

Robert 

Andrew Gerrand

unread,
Jan 4, 2012, 6:12:53 PM1/4/12
to golan...@googlegroups.com
Here's a dead simple script to turn any given text file into a .go
file with a constant string of the contents of the text file:

--

#!/bin/bash
# usage: goify package identifier source > outfile
echo "package $1"
echo -n "const $2 = "'`'
sed 's/`/`+"`"+`/g' $3
echo '`'

--

For example, this command line:
goify foo bar index.html
produces this output:
package foo
const bar = `index.html contents`

Andrew

Robert Johnstone

unread,
Jan 5, 2012, 12:06:22 PM1/5/12
to golang-nuts
The transfer is handled by your Makefile. For example, you can write
a dummy HTTP file server in a few lines of code. The makefile looks
like this:

include $(GOROOT)/src/Make.inc
TARG=example
GOFILES=main.go files.go
include $(GOROOT)/src/Make.cmd

files.go: index.html sub.html
fileembed -filename $@ $^

The first four lines are standard for making a command using Go. The
two additional lines at the end ensure that the source files are
converted to a Go source file. The automatic dependency checking of
make handles the rest. This is a very fast and easy way of ensuring
that the additional data files are present when your program runs.

If you are interested, look at the following link. It contains code
for a simple web server that uses embed source files.
https://bitbucket.org/rj/fileembed-go/src/bb663a7b296b/example

Good luck,

Robert
Reply all
Reply to author
Forward
0 new messages