Last Updated: February 25, 2016
· adnaan

Go Web App: Simple Resource Packaging Script

So you write your go web app, build a binary and think, "Now I can throw it all around the place for deployment". Well, not really. Go compiler does not package in your resources(which would be absolutely the right thing to do considering memory footprint). The resources have to be in the path of the executable binary to be found.

The following tools help you to do that:
1. nrsc
2. go-bindata

One other transparent way to do it would be to append your resources with the binary, move it to your deployment directory/server, extract and execute it. It can't get any simpler than that. I wrote up a bash script to do it.

$cd /path/to/myapp


#build your executable
go build -o myapp
# /view,/static,/conf directories
#zip the resources in
zip -r views static conf 
#append them to your executable binary
cat >> myapp
# Adjust  self-extracting  executable  archive
zip  --adjust-sfx myapp
#move to tmp dir or move it to your server
mv myapp ~/gotmp/myapp
#cd to the tmp dir
cd ~/gotmp
rm -r views static conf
#unzip the executable. or on server unzip it
unzip myapp
./myapp --myarg=MYARG

Make it executable

$chmod +x standalone



I hope the Go team has something up their sleeves for better resource packaging. That would be one killer feature.