go语言模版编程


传送门: 柏链项目学院html



go语言与模版编程

什么是模版编程

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

go语言模版编程步骤

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

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

模版编程可分为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就是建立模版:ui

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

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

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

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

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 main

import (
    "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,
Josie

Dear Uncle John,

It is a shame you couldn't make it to the wedding.
Thank you for the lovely moleskin pants.

Best wishes,
Josie

Dear Cousin Rodney,

It is a shame you couldn't make it to the wedding.

Best wishes,
Josie



相关文章
相关标签/搜索