Golang Template 使用自定义函数

Go 语言内置的 text/template 支持自定义函数功能:

// Funcs adds the elements of the argument map to the template's function map.
// It must be called before the template is parsed.
// It panics if a value in the map is not a function with appropriate return
// type or if the name cannot be used syntactically as a function in a template.
// It is legal to overwrite elements of the map. The return value is the template,
// so calls can be chained.
func (t *Template) Funcs(funcMap FuncMap) *Template {
	t.init()
	t.muFuncs.Lock()
	defer t.muFuncs.Unlock()
	addValueFuncs(t.execFuncs, funcMap)
	addFuncs(t.parseFuncs, funcMap)
	return t
}

简单理解,即通过 Funcs 函数调用,我们可以将自己定义的函数注册到 template 上,就可以在模版里通过 {{ 函数名 }} 方式引用了。云计算圈子里大家熟知的 Helm 就是采用这种方式实现的 yaml 函数渲染!

下面举个简单的栗子:

package main

import (
	"math/rand"
	"os"
	"sort"
	"text/template"

	"github.com/Masterminds/sprig/v3"
	"github.com/lithammer/dedent"
)

var demoTmpl = dedent.Dedent(`
	Hello, {{ userinfo }}!
	The date is {{ now | date "2006-01-02" }}.
	Generates a sorted random string {{ randomstring | sortstring | quote }}
`)

func main() {
	// initialize basic func map
	funcMap := sprig.TxtFuncMap()

	// add our special func `userinfo`
	funcMap["userinfo"] = func() string {
		return "SataQiu"
	}

	funcMap["randomstring"] = func() string {
		letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
		b := make([]rune, 12)
		for i := range b {
			b[i] = letterRunes[rand.Intn(len(letterRunes))]
		}
		return string(b)
	}

	funcMap["sortstring"] = func(s string) string {
		r := []rune(s)
		sort.Sort(sortRunes(r))
		return string(r)
	}

	ctx := map[string]interface{}{}

	template.Must(template.New("demo").Funcs(funcMap).Parse(demoTmpl)).Execute(os.Stdout, ctx)
}

type sortRunes []rune

func (s sortRunes) Less(i, j int) bool {
	return s[i] < s[j]
}

func (s sortRunes) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s sortRunes) Len() int {
	return len(s)
}

在这个例子中,我们一共使用了 6 个函数,其中有 3 个(now、date、quote)是 sprig 库已经为我们内置的基础函数,另外 3 个(userinfo、randomstring、sortstring)是我们自己编写的自定义函数。

该例的执行结果如下:

Hello, SataQiu!
The date is 2021-02-08.
Generates a sorted random string "GMVWWZapquvy"

可以看到,自定义函数已经被正确执行并渲染到模版里了!

附上在线运行地址:https://play.golang.org/p/2GDBe1LLrzA

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值