becky

Go asset embedding for use with `go generate`

  • 所有者: tv42/becky
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Becky -- Go asset embedding for use with go generate

Becky embeds assets as string literals in Go source.

Usage

Use becky via tools.go, see
https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module

For example, in your project create tools.go that has:

// +build tools

package tools

import (
	_ "github.com/tv42/becky"
)

And then near where you use the asset, put

//go:generate go run github.com/tv42/becky index.html

and run

$ go generate

This will create new files, named *.gen.go. You should add those
into your version control system, to ensure go get works for others.

You can pass multiple asset files at once, or repeat the go:generate
line.

Variable name

The generated files declare variables that now contain your assets.
Given the above index.html, the variable will be named index.

You can override the name with -var=NAME, or skip it with -var=_
and use side effects in your wrapper function (discussed later).

The asset will be an value of type asset (this code is
autogenerated, you don't need to type it in):

type asset struct {
	Name    string
	Content string
	...
}

Name has the original filename, as a hint for Content-Type
selection.

Wrapper

For most uses, an asset value needs to be given application or file
type specific functionality. To make this easy, the asset value will
be passed to a function caller wrapper. You need to write these
wrapper functions.

The name of the default wrapper is the (final) extension of the asset
filename. For index.html, that's html. You can override the
wrapper with -wrap=NAME.

In your application, you'd do something like

func html(a asset) http.Handler {
	return a
}

or

func txt(a asset) string {
	return a.Content
}

or

func tmpl(a asset) *template.Template {
	return template.Must(template.New(a.Name).Parse(a.Content))
}

to smartly handle *.html, *.txt and *.tmpl assets. Feel free
to pass the fields of asset to a factory function or type that
matches what you need, or use the asset, whatever suits your
project.

HTTP

Type asset implements http.Handler, including ETag cache
validation. It uses http.ServeContent which will set Content-Type
from the file name or content, and handle Range requests.

Code generation speed

If repeated calls to go run github.com/tv42/becky are too slow, you
can build it once and run from there:

//go:generate go build github.com/tv42/becky
//go:generate ./becky index.html

Build speed

gc, the Go compiler, can slow down with large source files. As e.g.
image assets can get big, this can start to slow down your builds. The
mechanism used for embedding has been chosen to be the most efficient
available.

Embedding a 10MB asset (creating a 28MB Go source file) takes <1
second to generate the code and about 1 second for every compilation.

You can minimize the number of times assets need to be compiled by
putting them in a different package that updates less often than most
of your source.

Development mode

If you build your application with -tags dev, asset.ServeHTTP will
reload the asset from disk on every request, and not use the embedded
copy. This makes editing HTML, CSS and such more convenient.

主要指標

概覽
名稱與所有者tv42/becky
主編程語言Go
編程語言Go (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2014-11-26 00:40:13
推送於2021-03-03 19:40:27
最后一次提交2021-03-03 12:40:19
發布數0
用户参与
星數108
關注者數3
派生數5
提交數30
已啟用問題?
問題數6
打開的問題數0
拉請求數0
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?