We can package any files/directories using the pack
command of the gf
command line tool. For specific installation and usage of the gf
command line tool, please refer to the Resource Packing section. Since it is quite convenient to package using the command line tool, it is the recommended packaging method.
gf pack
Generates Go
File
The recommended way is to directly generate the Go
file to the boot
startup directory and set the package name of the generated Go
file to boot
, so the resource file will be automatically introduced into the project. We pack the files of the project’s config, public, template
directories into a Go
file, the packaging command is:
gf pack config,public,template packed/data.go -n packed
The content of the generated Go
file is similar to:
package packed
import "github.com/gogf/gf/v2/os/gres"
func init() {
if err := gres.Add("H4sIAAAAAAAC/5y8c5Bl0Zbuu9O2bVaq0rZZ6Urbtm3bNnfatipto9"); err != nil {
panic(err)
}
}
As you can see, the generated Go
file adds the binary content of the resource file to the default resource manager through the gres.Add
method. The parameter of this method is a compressed BASE64 string, which will be decompressed during program startup and generate a file tree object in memory for quick file manipulation during runtime.
Using the Packaged Go
File
Prioritize Importing packed
Resource Package in boot
Package
In the project’s boot
program startup setting package, automatically import the packed
resource package, and it should be the first package to be imported, so that other imported packages can use the resource content during initialization (init
method). For example (the module
name is my-app
):
import (
_ "my-app/packed"
// other packages
)
It is recommended to include a blank line between the packed
package and other packages, especially since the Goland
IDE’s import
plugin will not automatically sort the imported packages.
Prioritize Importing boot
Package in main
Package
Since the project’s main
entry program file will import the boot
package, and it should be the first package to be imported:
import (
_ "my-app/boot"
// other packages
)
Here, it is recommended to include a blank line between the boot
package and other packages for distinction, especially since the Goland
IDE’s import
plugin will not automatically sort the imported packages.
Then you can use the gres
module anywhere in the project to access the packaged resource files.
If using the recommended project directory structure of
GoFrame
(new project), the directory structure will have aboot
directory (corresponding package name is alsoboot
) for program startup settings. Therefore, if theGo
file is generated in theboot
directory, it will be automatically compiled into the executable file.
Print Resource Management File List
The gres.Dump()
method can be used to print out all file lists in the current resource manager, with output similar to:
2019-09-15T13:36:28+00:00 0.00B config
2019-07-27T07:26:12+00:00 1.34K config/config.toml
2019-09-15T13:36:28+00:00 0.00B public
2019-06-25T17:03:56+00:00 0.00B public/resource
2018-12-04T12:50:16+00:00 0.00B public/resource/css
2018-12-17T12:54:26+00:00 0.00B public/resource/css/document
2018-12-17T12:54:26+00:00 4.20K public/resource/css/document/style.css
2018-08-24T01:46:58+00:00 32.00B public/resource/css/index.css
2019-05-23T03:51:24+00:00 0.00B public/resource/image
2018-08-20T05:02:08+00:00 24.01K public/resource/image/cover.png
2019-05-23T03:51:24+00:00 4.19K public/resource/image/favicon.ico
2018-08-23T01:44:50+00:00 4.19K public/resource/image/gf.ico
2018-12-04T13:04:34+00:00 0.00B public/resource/js
2019-06-27T11:06:12+00:00 0.00B public/resource/js/document
2019-06-27T11:06:12+00:00 11.67K public/resource/js/document/index.js
2019-09-15T13:36:28+00:00 0.00B template
2019-02-02T09:08:56+00:00 0.00B template/document
2018-12-04T12:49:08+00:00 0.00B template/document/include
2018-12-04T12:49:08+00:00 329.00B template/document/include/404.html
2019-03-06T01:52:56+00:00 3.42K template/document/index.html
...
Note that when using resource files in the resource manager, you need to strictly follow their paths for retrieval.