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:

  1. gf pack config,public,template packed/data.go -n packed

The content of the generated Go file is similar to:

  1. package packed
  2. import "github.com/gogf/gf/v2/os/gres"
  3. func init() {
  4. if err := gres.Add("H4sIAAAAAAAC/5y8c5Bl0Zbuu9O2bVaq0rZZ6Urbtm3bNnfatipto9"); err != nil {
  5. panic(err)
  6. }
  7. }

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):

  1. import (
  2. _ "my-app/packed"
  3. // other packages
  4. )

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:

  1. import (
  2. _ "my-app/boot"
  3. // other packages
  4. )

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 a boot directory (corresponding package name is also boot) for program startup settings. Therefore, if the Go file is generated in the boot 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:

  1. 2019-09-15T13:36:28+00:00 0.00B config
  2. 2019-07-27T07:26:12+00:00 1.34K config/config.toml
  3. 2019-09-15T13:36:28+00:00 0.00B public
  4. 2019-06-25T17:03:56+00:00 0.00B public/resource
  5. 2018-12-04T12:50:16+00:00 0.00B public/resource/css
  6. 2018-12-17T12:54:26+00:00 0.00B public/resource/css/document
  7. 2018-12-17T12:54:26+00:00 4.20K public/resource/css/document/style.css
  8. 2018-08-24T01:46:58+00:00 32.00B public/resource/css/index.css
  9. 2019-05-23T03:51:24+00:00 0.00B public/resource/image
  10. 2018-08-20T05:02:08+00:00 24.01K public/resource/image/cover.png
  11. 2019-05-23T03:51:24+00:00 4.19K public/resource/image/favicon.ico
  12. 2018-08-23T01:44:50+00:00 4.19K public/resource/image/gf.ico
  13. 2018-12-04T13:04:34+00:00 0.00B public/resource/js
  14. 2019-06-27T11:06:12+00:00 0.00B public/resource/js/document
  15. 2019-06-27T11:06:12+00:00 11.67K public/resource/js/document/index.js
  16. 2019-09-15T13:36:28+00:00 0.00B template
  17. 2019-02-02T09:08:56+00:00 0.00B template/document
  18. 2018-12-04T12:49:08+00:00 0.00B template/document/include
  19. 2018-12-04T12:49:08+00:00 329.00B template/document/include/404.html
  20. 2019-03-06T01:52:56+00:00 3.42K template/document/index.html
  21. ...

Note that when using resource files in the resource manager, you need to strictly follow their paths for retrieval.