Life goes on and on go Go go!!!
When you use Go to develop an app, you sometimes encounter situations where you need to read static resources. For example, to develop WEB applications, the program needs to load the HTML of the template file generation output. In the program deployment, in addition to publishing the application executable file, you also need to publish the dependent static resource files. This adds some trouble to the publishing process. Since publishing a single executable is a very simple operation, someone will find a way to package the static resource file into the Go program file.
Reference Address:
Http://fuxiaohei.me/2016/10/1/go-binary-embed-asset.html
The article mentions:
Go-bindata
Go.rice
Esc
This blog will only introduce Go.rice, the rest will be introduced later.
What ' s an Embedded Resource?
An embedded resource in a application are a file that's included as part of the application. The file is not a compiled, but was accessable from the code at Run-time. Embedded resources can is any file type.
Languages as JAVA and C # Support resources out of box. However, this is not the case for Golang. In order to emebed resource, we need to develop our own solution. Thankfully, there is couple of tools that is doing this for us.
Reference Address:
http://blog.ralch.com/tutorial/golang-embedded-resources/ Go.rice
Go.rice is a Go package the makes working with resources such as html,js,css,images,templates, etc very easy.
GitHub Address:
Https://github.com/GeertJohan/go.rice
star:1107
Get:
Go get github.com/geertjohan/go.rice
go get github.com/geertjohan/go.rice/rice
Findbox
Funcation to access a particular resource bundler (directory).
The function is finding the correct absolute path for your resource files.
Find a rice. Box
Templatebox, err: = Rice. Findbox ("Your-resource-directory")
if err! = Nil {
log. Fatal (Err)
}
//Get file contents As String
Tmpl, err: = Templatebox.string ("Your_asset.tmpl")
If Err! = nil {
log. Fatal (err)
}
Embedded Resource as source code
Embed resources as source code
Command:
Rice embed-go
Generate File:
<directory-name>.rice-box.go
Embedded Resource as an archive
Appends a resource as a zip file to already built executable
Append to an already existing executable file as a zip
Embedded Resource as an SYSO resource
This is experimental method, that generates. syso file, is compiled by Go compiler. The following command generates the COFF syso resource files per directory:
Rice EMBED-SYSO
Go build-o <program> Rice
append--exec <program>
use Go.rice in Echo
Code Main.go:
Package main
Import (
"Net/http" "Github.com/geertjohan/go.rice" "
github.com/labstack/echo
"
Func Main () {
e: = echo. New ()
//The file server for rice. The "app" is the folder where the files come from.
Assethandler: = http. Fileserver (rice. Mustfindbox ("App"). Httpbox ())
//serves the index.html from Rice
e.get ("/", Echo. Wraphandler (Assethandler))
//Servers other static files
e.get ("/static/*", Echo. Wraphandler (http. Stripprefix ("/static/", Assethandler))
e.logger.fatal (E.start (": 1323"))
}
same level with Main.go, create a new folder app, put in the file file.txt
Perform:
Rice embed-go
The Rice-box.go is generated:
Package main
Import (
"github.com/geertjohan/go.rice/embedded"
"Time"
)
func init () {
// Define files
file2: = &embedded. embeddedfile{
Filename: "file.txt",
filemodtime:time. Unix (1511406219, 0),
Content: string (""),
}
//define dirs
Dir1: = &embedded. embeddeddir{
Filename: "",
dirmodtime:time. Unix (1511406219, 0),
childfiles: []*embedded. embeddedfile{
file2,//"file.txt"
},
}
//link childdirs
dir1. Childdirs = []*embedded. embeddeddir{}
//register Embeddedbox
Embedded. Registerembeddedbox (' app ', &embedded. embeddedbox{
Name: ' app ',
time:time. Unix (1511406219, 0),
dirs:map[string]*embedded. embeddeddir{
"": Dir1,
},
files:map[string]*embedded. embeddedfile{
"file.txt": File2,
},}
)
}
Perform:
Go Build
Build file: Embed_resources.exe
Run Embed_resources.exe
Delete File.txt under the app folder
Browser Access http://localhost:1323/
Can see the file.txt file