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.

 

你可能感兴趣的:(go学习)