1 Star 0 Fork 1

Bin/gotemplate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
YouBin 提交于 2025-02-27 10:08 +08:00 . 增加对trim语法的支持
package gotemplate
import (
"fmt"
"regexp"
"strings"
"golang.org/x/sync/errgroup"
)
// 对反引号进行转义
func escapeBacktick(content *string) {
// 由于go的标签内无法直接包含反引号,所以只能用字符串拼接的方式进行转义
*content = strings.ReplaceAll(*content, "`", "`+\"`\"+`")
}
// 对代码进行转义
func encodeCode(content *string) {
re := regexp.MustCompile(`\{\{|\}\}`)
*content = re.ReplaceAllStringFunc(*content, func(s string) string {
if s == "{{" {
return "@{"
} else if s == "}}" {
return "@}"
}
return s
})
}
func decodeCode(content *string) {
re := regexp.MustCompile(`@\{|@\}`)
*content = re.ReplaceAllStringFunc(*content, func(s string) string {
if s == "@{" {
return "{{"
} else if s == "@}" {
return "}}"
}
return s
})
}
// 错误组
type ErrGroup struct {
errgroup.Group
}
func (e *ErrGroup) Go(fn func() error) {
e.Group.Go(func() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
return fn()
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/llyb120/gotemplate.git
git@gitee.com:llyb120/gotemplate.git
llyb120
gotemplate
gotemplate
master

搜索帮助