博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go语言模版编程
阅读量:7085 次
发布时间:2019-06-28

本文共 3361 字,大约阅读时间需要 11 分钟。

1240

传送门:


go语言与模版编程

什么是模版编程

模板是将一个事物的结构规律予以固定化、标准化的成果,它体现的是结构形式的标准化。对于我们程序员来说,更直白的理解是;对于要输出的内容,个人位置需要参数化填充,需要填充的参数位置是固定的,输出的内容是有统一标准的。就像是我们小时候做的填空题,题目固定,有固定的位置需要填充,模版就是原理。

go语言模版编程步骤

go语言开发了专门的packge用于支持模版编程,我们开发过程中,有时候会碰到模版编程得到情况,比如想要实现自动化的输出一些用于其他工程执行的有效代码,或者形成一些前端页面,都会用到模版编程。

go语言中模版编程分为两个大方向,一种是真的前端的,是在html/template下,还有一个是针对文本的,在text/template下,两者套路类似,我们来说一下对于文本的处理。

模版编程可分为4步:

  • 定义模版
  • 创建模版
  • 模版数据准备
  • 模版执行

我们可以先看官方示例给的代码,把步骤重新认识一遍。

type Inventory struct {    Material string    Count    uint}sweaters := Inventory{"wool", 17}tmpl, err := template.New("test").Parse("{
{.Count}} items are made of {
{.Material}}")if err != nil { panic(err) }err = tmpl.Execute(os.Stdout, sweaters)if err != nil { panic(err) }

所谓的定义模版就是Parse中的内容:

{
{.Count}} items are made of {
{.Material}}

template调用New以及后面的Parse就是创建模版:

tmpl, err := template.New("test").Parse("{
{.Count}} items are made of {
{.Material}}")if err != nil { panic(err) }

模版数据的准备是指结构定义和数据准备:

type Inventory struct {    Material string    Count    uint}sweaters := Inventory{"wool", 17}

模版执行是Execute,注意模版执行时需要指定输出描述符(可以是某个文件或者标准输出,对应的go语言中的io.writer)

err = tmpl.Execute(os.Stdout, sweaters)

上述例子就是一个最为简单的模版编程,更详尽的可以看这里:

上述例子除了阐述模版编程的四步之外,还有一个重要信息,模版编程在执行的时候,对数据结构体有要求,模版使用{

{}}包含的变量名字必须在结构体有其对应,否则执行的时候将不能成功。

下面是模版编程里的一些actions介绍。

{
{/* a comment */}} 注释{
{pipeline}} 模版的变量{
{if pipeline}} T1 {
{end}} 如果pipeline为空,则T1不会执行,{
{if pipeline}} T1 {
{else}} T0 {
{end}} 这是在上面的基础上增加了else分支{
{if pipeline}} T1 {
{else if pipeline}} T0 {
{end}} 当然还可以else if,这些对熟悉编程的人来说不算什么{
{range pipeline}} T1 {
{end}} 这个要求pipeline是一个数组,可以循环输出T1{
{range pipeline}} T1 {
{else}} T0 {
{end}} 这个就是pipeline为空则执行T0{
{with pipeline}} T1 {
{end}} with和if类似,如果pipeline存在,则执行T1{
{with pipeline}} T1 {
{else}} T0 {
{end}} 同样的else分支处理

我们来看一些复杂点的例子:

package mainimport (    "fmt"    "os"    "text/template")// Define a template.const letter = `Dear {
{.Name}},{
{if .Attended}}It was a pleasure to see you at the wedding.{
{else}}It is a shame you couldn't make it to the wedding.{
{end}}{
{with .Gift}}Thank you for the lovely {
{.}}.{
{end}}Best wishes,Josie`func main() { // Prepare some data to insert into the template. type Recipient struct { Name, Gift string Attended bool } var recipients = []Recipient{ {"Aunt Mildred", "bone china tea set", true}, {"Uncle John", "moleskin pants", false}, {"Cousin Rodney", "", false}, } // Create a new template and parse the letter into it. t := template.Must(template.New("letter").Parse(letter)) // Execute the template for each recipient. for _, r := range recipients { err := t.Execute(os.Stdout, r) if err != nil { fmt.Println("executing template:", err) } }}

上述代码是一个写信的模版,收信方是一个固定填充位置,Attended是一个附加信息,如果为真,则为受邀参加婚礼,为假则不被邀请。Gift则是对礼物的描述,如果礼物不为空,则该收信人会有礼物,否则没有。

根据数据情况recipients来看,只有第一个人受邀参加婚礼,前两个人会有礼物,所以最终的执行结果如下:

Dear Aunt Mildred,It was a pleasure to see you at the wedding.Thank you for the lovely bone china tea set.Best wishes,JosieDear Uncle John,It is a shame you couldn't make it to the wedding.Thank you for the lovely moleskin pants.Best wishes,JosieDear Cousin Rodney,It is a shame you couldn't make it to the wedding.Best wishes,Josie


%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20190103092604.jpg

转载于:https://www.cnblogs.com/tokenpai/p/10643929.html

你可能感兴趣的文章
CSS3阴影 box-shadow的使用和技巧总结
查看>>
easyui datagrid 初始化排序按钮不显示的问题
查看>>
5分钟GET一场清华大数据讲座实战应用案例干货
查看>>
测者的测试技术手册:自动的自动化框架EvoSuite集成Cobertura得到可视化的代码覆盖报告...
查看>>
集成LinearLayout时遇到的问题
查看>>
设计模式之——策略模式(Strategy)
查看>>
单利模式的好处
查看>>
Microsoft Silverlight 官方版
查看>>
Hibernate中*.hbm.xml文件让其自动读取数据库默认值
查看>>
windows 安装redis
查看>>
oracle常用函数汇总
查看>>
==和===的区别
查看>>
.bat文件调用java类的main方法
查看>>
SpringMVC 4.2 对跨域的支持
查看>>
如何防御CC攻击之我见
查看>>
Hive常用HiveQL
查看>>
Wireshark鸡毛蒜皮
查看>>
Ubuntu 系统grub丢失,不能进入系统 或者只能进入windows系统的dos界面解决办法
查看>>
整合log4j和mybatis,输出最终生成的SQL
查看>>
unix 系统调用和库函数的文档
查看>>