72 lines
1.1 KiB
Go
72 lines
1.1 KiB
Go
package tmpl
|
||
|
||
import (
|
||
"encoding/json"
|
||
"strings"
|
||
"text/template"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
func New(app *gin.Engine) {
|
||
app.Static("/assets", "./res/assets")
|
||
|
||
funcMap := template.FuncMap{
|
||
"mod": GetRemainder,
|
||
"add": GetAdd,
|
||
"div": GetDivision,
|
||
"sub": Sub,
|
||
"seq": Seq,
|
||
"mul": GetMultiply,
|
||
"test": TestFunc,
|
||
}
|
||
app.SetFuncMap(funcMap)
|
||
|
||
app.LoadHTMLGlob("./res/templates/*")
|
||
}
|
||
|
||
func TestFunc(in string) string {
|
||
return strings.ToUpper(in)
|
||
}
|
||
|
||
func AnyToBytes(data any) ([]byte, error) {
|
||
return json.Marshal(data)
|
||
}
|
||
|
||
// 定义取余函数
|
||
func GetRemainder(a, b int) int {
|
||
return a % b
|
||
}
|
||
|
||
// 定义 整数相加
|
||
func GetAdd(a, b int) int {
|
||
return a + b
|
||
}
|
||
|
||
// 定义 整数相乘
|
||
func GetMultiply(a, b int) int {
|
||
return a * b
|
||
}
|
||
|
||
// 定义 整数相除
|
||
func GetDivision(a, b int) int {
|
||
if b == 0 {
|
||
return 0 // 当除数为 0 时返回 0,避免 panic
|
||
}
|
||
return a / b
|
||
}
|
||
|
||
// 定义 整数相减
|
||
func Sub(a, b int) int {
|
||
return a - b
|
||
}
|
||
|
||
// 定义 获取页码
|
||
func Seq(start, end int) []int {
|
||
result := make([]int, end-start+1)
|
||
for i := range result {
|
||
result[i] = start + i
|
||
}
|
||
return result
|
||
}
|