我们可以通过gf命令行工具的pack命令实现对任意文件/目录的打包,关于gf命令行工具的安装和使用具体请查看 开发工具 章节。由于通过命令行工具进行打包比较简便,因此也是推荐的打包方式。
gf pack的命令介绍如下:
$ gf pack -hUSAGEgf pack SRC DSTARGUMENTSRC source path for packing, which can be multiple source paths.DST destination file path for packed file. if extension of the filename is ".go" and "-n" option is given,it enables packing SRC to go file, or else it packs SRC into a binary file.OPTION-n, --name package name for output go file, it's set as its directory name if no name passed-p, --prefix prefix for each file packed into the resource fileEXAMPLESgf pack public data.bingf pack public,template data.bingf pack public,template packed/data.gogf pack public,template,config packed/data.gogf pack public,template,config packed/data.go -n=packed -p=/var/www/my-appgf pack /var/www/public packed/data.go -n=packed
1. gf pack生成Go文件
比较推荐的方式是将Go文件直接生成到boot启动目录,并设置生成Go文件的包名为boot,这样该资源文件将会被自动引入到项目中。我们将项目的config,public,template三个目录的文件打包到Go文件,打包命令为:
gf pack config,public,template packed/data.go -n packed
生成的Go文件内容类似于:
package packedimport "github.com/gogf/gf/os/gres"func init() {if err := gres.Add("H4sIAAAAAAAC/5y8c5Bl0Zbuu9O2bVaq0rZZ6Urbtm3bNnfatipto9"); err != nil {panic(err)}}
可以看到,生成的Go文件中通过gres.Add方法将资源文件的二进制内容添加到默认的资源管理器中,该方法的参数是压缩过后的BASE64字符串,将会在程序启动的时候做解压并在内存中生成一个文件树对象,便于在运行时快速操作文件。
2. 使用打包的Go文件
1) 在boot包中优先引入packed资源包
在项目的boot程序启动设置包中自动引入packed资源包,并且应当作为第一个引入的包,以便于其他引入的包在初始化时(init方法中)便能使用到资源内容,例如像这样(module名称为my-app):
import (_ "my-app/packed"// 其他包)
这里建议引入packed包和其他包之间加入一个空行以作区分,特别是Goland IDE的import插件不会将引入包进行自动排序。
2) 在main包中优先引入boot包
由于项目的main入口程序文件会引入boot包,并且应当作为第一个引入的包:
import (_ "my-app/boot"// 其他包)
这里建议引入boot包和其他包之间加入一个空行以作区分,特别是Goland IDE的import插件不会将引入包进行自动排序。
随后可以在项目的任何地方使用gres模块来访问打包的资源文件。
如果使用GoFrame推荐的项目目录结构(新建项目),在目录结构中会存在boot目录(对应包名也是boot),用于程序启动设置。因此如果将Go文件生成到boot目录下,那么将会被自动编译进可执行文件中。
3. 打印资源管理文件列表
可以通过gres.Dump()方法打印出当前资源管理器中所有的文件列表,输出内容类似于:
2019-09-15T13:36:28+00:00 0.00B config2019-07-27T07:26:12+00:00 1.34K config/config.toml2019-09-15T13:36:28+00:00 0.00B public2019-06-25T17:03:56+00:00 0.00B public/resource2018-12-04T12:50:16+00:00 0.00B public/resource/css2018-12-17T12:54:26+00:00 0.00B public/resource/css/document2018-12-17T12:54:26+00:00 4.20K public/resource/css/document/style.css2018-08-24T01:46:58+00:00 32.00B public/resource/css/index.css2019-05-23T03:51:24+00:00 0.00B public/resource/image2018-08-20T05:02:08+00:00 24.01K public/resource/image/cover.png2019-05-23T03:51:24+00:00 4.19K public/resource/image/favicon.ico2018-08-23T01:44:50+00:00 4.19K public/resource/image/gf.ico2018-12-04T13:04:34+00:00 0.00B public/resource/js2019-06-27T11:06:12+00:00 0.00B public/resource/js/document2019-06-27T11:06:12+00:00 11.67K public/resource/js/document/index.js2019-09-15T13:36:28+00:00 0.00B template2019-02-02T09:08:56+00:00 0.00B template/document2018-12-04T12:49:08+00:00 0.00B template/document/include2018-12-04T12:49:08+00:00 329.00B template/document/include/404.html2019-03-06T01:52:56+00:00 3.42K template/document/index.html...
需要注意的是,在使用资源管理器中的资源文件时,需要严格按照其路径进行检索获取。
