glang template使用

test.template

{{. | printadd}}
{{. | printupdate}}
{{. | printdel}}

test.go

package main

import (
	"fmt"
	"html/template"
	"os"
)

type TemplateInfo struct {
	OperateType string
	NewRecord   string
	OldRecord   string
	Prefix      string
	ZoneName    string
}

func main() {
	data1 := &TemplateInfo{
		OperateType: "ADD",
		NewRecord:   "mc.vip.vip.com 1.1.1.1 a",
		OldRecord:   "",
		Prefix:      "",
		ZoneName:    "",
	}

	data2 := &TemplateInfo{
		OperateType: "UPDATE",
		NewRecord:   "mc.vip.vip.com 3.3.3.3 a",
		OldRecord:   "mc.vip.vip.com 2.2.2.2 a",
		Prefix:      "",
		ZoneName:    "",
	}

	data3 := &TemplateInfo{
		OperateType: "DEL",
		NewRecord:   "mc.vip.vip.com 3.3.3.3 a",
		OldRecord:   "",
		Prefix:      "",
		ZoneName:    "",
	}

	data4 := &TemplateInfo{
		OperateType: "ADD",
		NewRecord:   "mc.vip.vip.com 11.11.11.11 a",
		OldRecord:   "",
		Prefix:      "",
		ZoneName:    "",
	}

	zonelist := map[string][]*TemplateInfo{}
	zonelist["vip.vip.com"] = append(zonelist["vip.vip.com"], data1)
	zonelist["vip.vip.com"] = append(zonelist["vip.vip.com"], data2)
	zonelist["vip.vip.com"] = append(zonelist["vip.vip.com"], data3)
	zonelist["vip.vip.com"] = append(zonelist["vip.vip.com"], data4)

	printAdd := func(paratype string) string {
		text := ""
		addFlag := false
		for _, v := range zonelist[paratype] {
			if v.OperateType == "ADD" {
				if addFlag == false {
					text = "新增记录:\n"
					text += v.NewRecord + "\n"
					addFlag = true
				} else {
					text += v.NewRecord + "\n"
				}
			}
		}
		return text

	}

	printUpdate := func(paratype string) string {
		text := ""
		updateFlag := false
		for _, v := range zonelist[paratype] {
			if v.OperateType == "UPDATE" {
				if updateFlag == false {
					text = "修改记录:\n"
					text += "原纪录:" + v.OldRecord + "\n"
					text += "新纪录:" + v.NewRecord + "\n"
					updateFlag = true
				} else {
					text += "原纪录:" + v.OldRecord + "\n"
					text += "新纪录:" + v.NewRecord + "\n"
				}
			}
		}

		return text
	}

	printDel := func(paratype string) string {
		text := ""
		addFlag := false
		for _, v := range zonelist[paratype] {
			if v.OperateType == "DEL" {
				if addFlag == false {
					text = "删除记录:\n"
					text += v.NewRecord + "\n"
					addFlag = true
				} else {
					text += v.NewRecord + "\n"
				}
			}
		}
		return text
	}

	//var t *template.Template

	t := template.New("test.template")
	template.Must(t.Funcs(template.FuncMap{"printadd": printAdd, "printupdate": printUpdate, "printdel": printDel}).ParseFiles("test.template"))
	for k, _ := range zonelist {
		err := t.Execute(os.Stdout, k)
		if err != nil {
			fmt.Println("executing template err:", err)
		}
	}
}

运行结果:

D:/go1.10.1.windows-amd64/go/bin/go.exe build -i [D:/Goproject/src/testmap]
成功: 进程退出代码 0.
D:/Goproject/src/testmap/testmap.exe  [D:/Goproject/src/testmap]
新增记录:
mc.vip.vip.com 1.1.1.1 a
mc.vip.vip.com 11.11.11.11 a

修改记录:
原纪录:mc.vip.vip.com 2.2.2.2 a
新纪录:mc.vip.vip.com 3.3.3.3 a

删除记录:
mc.vip.vip.com 3.3.3.3 a
成功: 进程退出代码 0.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值