go.rich 静态资源嵌入

    技术2022-07-10  152

    文章目录

    什么是go.rich如何使用作为源码嵌入资源嵌入:追加 rice append

    什么是go.rich

    官网: https://github.com/GeertJohan/go.rice

    go.rice是一个go软件包,可以很方便的处理html、js、css、图像、模版、配置文件等静态资源文件,在开发调试过程中,可以直接从磁盘加载所需的文件,生成可执行程序后,在不修改源代码的情况下,将资源文件添加到可执行程序中。

    总结: go.rice是一个golang 静态资源嵌入工具包。go.rice 支持打包静态文件到 go 文件中。从使用角度,go.rice 其实是更便捷的静态文件操作库。

    功能:

    找到资源文件正确的绝对路径。比如你在home目录执行你的二进制程序,程序依赖一个html-fles的文件夹(包含html等资源),但是html-files在中$GOPATH/src/yourApplication/html-files中,你的程序只是调用rice.FindBox(“html-files”)就好,go.rice将查找到该目录的正确路径(相对于yourApplication的位置)。

    **将资源文件嵌入到二进制程序,不再从文件系统加载资源文件,这将创建一个“独立”可执行文件,减少对文件系统的依赖。**资源文件可以在编译你的源代码时可以将资源文件转换成go源文件嵌入,也可以在源码编译成二进制后追加,你的程序里都能通过rice.FindBox找到资源。

    如何使用

    注意:调用FindBox()或MustFindBox()时参数必须要使用字符串,例如FindBox(“example”),不能使用字符串常量或变量,否则将报错。MustFindBox和FindBox功能一样,只不过MustFindBox如果出错会直接panic。

    作为源码嵌入

    rice embed-go go build

    执行 rice embed-go后,会在当前目录生成一个rice-box.go的go源文件,这个源文件包含所有资源文件,通过go build再将其编译成二进制,生成的go源文件会变大,会减慢编译速度并需要更多内存来进行编译。

    go.rice 把一个目录认为是一个 rice.Box 操作:

    import ( "fmt" "html/template" "github.com/GeertJohan/go.rice" ) func main() { // 这里写相对于的执行文件的地址 box, err := rice.FindBox("theme/default") if err != nil { println(err.Error()) return } // 从目录 Box 读取文件 str, err := box.String("post.html") if err != nil { println(err.Error()) return } t, err := template.New("tpl").Parse(str) fmt.Println(t, err) }

    go.rice 的打包命令是 rice。用起来非常直接:在有使用 go.rice 操作的 go 代码目录,直接执行 rice embed-go:

    rice 命令 go.rice 的打包命令是 rice。用起来非常直接:在有使用 go.rice 操作的 go 代码目录,直接执行 rice embed-go:

    rice embed-go rice -i "github.com/xxx/xyz" embed-go // -i 处理指定包里的 go.rice 操作

    它就会生成当前包名下的、嵌入了文件的代码 rice-box.go。但是,它不递归处理 import。他会分析当前目录下的 go 代码中 go.rice 的使用,找到对应需要嵌入的文件夹。但是子目录下的和 import 的里面的 go.rice 使用不会分析,需要你手动 cd 过去或者 -i 指定要处理的包执行命令。这点来说非常的不友好。

    资源嵌入:追加 rice append

    # go run github.com/GeertJohan/go.rice/rice --help Usage: rice [OPTIONS] <command> Application Options: --memprofile= Write memory profile to this file --cpuprofile= Write cpu profile to this file -v, --verbose Show verbose debug information -i, --import-path= Import path(s) to use. Using PWD when left empty. Specify multiple times for more import paths to append Help Options: -h, --help Show this help message Available commands: append clean embed-go embed-syso

    rice append:将资源作为zip文件附加到可执行文件中,此方法更改已生成的可执行文件。它将资源作为zip文件附加到二进制文件中。 它使编译速度更快。使用append方法可以很好地向可执行二进制文件添加大型assets 。

    附加的缺点是它没有提供工作查找方法(a working Seek method)。

    makefile 中使用如下:

    taigu: $(BUILD_DEPS) @echo $(GOFLAGS) rm -f taigu go build $(GOFLAGS) -o taigu ./cmd/taigu go run github.com/GeertJohan/go.rice/rice append --exec taigu -i ./build .PHONY: taigu BINS+=taigu

    我们可以使用-i 参数添加一个路径,它会在-i 添加的路径下go文件中找,rice.FindBox() or rice.MustFindBox()函数调用。如果目录下,没有该函数调用,会报如下信息:

    go run github.com/GeertJohan/go.rice/rice append --exec taigu -i ./build no calls to rice.FindBox() or rice.MustFindBox() found in import path `./build`
    Processed: 0.014, SQL: 9