esc - 嵌入文件到Go程序并提供http.FileSystem接口给他们

网友投稿 684 2022-10-26

esc - 嵌入文件到Go程序并提供http.FileSystem接口给他们

esc - 嵌入文件到Go程序并提供http.FileSystem接口给他们

esc

esc embeds files into go programs and provides http.FileSystem interfaces to them.

It adds all named files or files recursively under named directories at the path specified. The output file provides an http.FileSystem interface with zero dependencies on packages outside the standard library.

Installation

go get -u github.com/mjibson/esc

Usage

esc [flag] [name ...]

The flags are:

-o="" output filename, defaults to stdout-pkg="main" package name of output file, defaults to main-prefix="" strip given prefix from filenames-ignore="" regular expression for files to ignore-include="" regular expression for files to include-modtime="" Unix timestamp to override as modification time for all files-private unexport functions by prefixing them with esc, e.g. FS -> escFS-no-compress do not compress files

Accessing Embedded Files

After producing an output file, the assets may be accessed with the FS() function, which takes a flag to use local assets instead (for local development).

(_esc)?FS(Must)?(Byte|String) returns an asset as a (byte slice|string).(_esc)?FSMust(Byte|String) panics if the asset is not found.

Go Generate

esc can be invoked by go generate:

//go:generate esc -o static.go -pkg server static

Example

Embedded assets can be served with HTTP using the http.FileServer. Assuming you have a directory structure similar to the following:

.├── main.go└── static ├── css │   └── style.css └── index.html

Where main.go contains:

package mainimport ( "log" "net/http")func main() { // FS() is created by esc and returns a http.Filesystem. http.Handle("/static/", http.FileServer(FS(false))) log.Fatal(http.ListenAndServe(":8080", nil))}

Generate the embedded data: esc -o static.go staticStart the server: go run main.go static.goAccess http://localhost:8080/static/index.html to view the files.

You can see worked example in example dir just run it as go run example/main.go example/static.go

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Tomcat Servlet内存马
下一篇:leetcode刷题,我推荐B站这个妹子学霸的视频
相关文章

 发表评论

暂时没有评论,来抢沙发吧~