1. 程式人生 > >Go標準庫:深入剖析Go template

Go標準庫:深入剖析Go template

本文只關注Go text/template的底層結構,帶上了很詳細的圖片以及示例幫助理解,有些地方也附帶上了原始碼進行解釋。有了本文的解釋,對於Go template的語法以及html/template的用法,一切都很簡單。

入門示例

package main

import (
    "html/template"
    "os"
)

type Person struct {
    Name string
    Age    int
}

func main() {
    p := Person{"longshuai", 23}
    tmpl, err := template.New("test").Parse("Name: {{.Name}}, Age: {{.Age}}")
    if err != nil {
        panic(err)
    }
    err = tmpl.Execute(os.Stdout, p)
    if err != nil {
        panic(err)
    }
    fmt.Println(tmpl)
}

上面定義了一個Person結構,有兩個大寫字母開頭(意味著這倆欄位是匯出的)的欄位Name和Age。然後main()中建立了Person的例項物件p。

緊接著使用template.New()函式建立了一個空Template例項(物件),然後通過這個template例項呼叫Parse()方法,Parse()方法用來解析、評估模板中需要執行的action,其中需要評估的部分都使用{{}}包圍,並將評估後(解析後)的結果賦值給tmpl。

最後呼叫Execute()方法,該方法將資料物件Person的例項p應用到已經解析的tmpl模板,最後將整個應用合併後的結果輸出到os.Stdout。

上面的示例很簡單,兩個注意點:

  1. 流程:構建模板物件New()-->解析資料Parse()-->應用合併Execute()
  2. Parse()解析的物件中包含了{{}},其中使用了點(.),{{.Name}}代表Execute()第二個引數p物件的Name欄位,同理{{.Age}}

也就是說,{{.}}代表的是要應用的物件,類似於java/c++中的this,python/perl中的self。

更通用地,{{.}}表示的是所處作用域的當前物件,而不僅僅只代表Execute()中的第二個引數物件。例如,本示例中{{.}}代表頂級作用域的物件p,如果Parse()中還有巢狀的作用域range,則{{.}}

代表range迭代到的每個元素物件。如果瞭解perl語言,{{.}}可以理解為預設變數$_

模板關聯(associate)

template中有不少函式、方法都直接返回*Template型別。

上圖中使用紅色框線框起來一部分返回值是*Template的函式、方法。對於函式,它們返回一個Template例項(假設為t),對於使用t作為引數的Must()函式和那些框起來的Template方法,它們返回的*Template其實是原始例項t

例如:

t := template.New("abc")
tt,err := t.Parse("xxxxxxxxxxx")

這裡的t和tt其實都指向同一個模板物件。

這裡的t稱為模板的關聯名稱。通俗一點,就是建立了一個模板,關聯到變數t上。但注意,t不是模板的名稱,因為Template中有一個未匯出的name欄位,它才是模板的名稱。可以通過Name()方法返回name欄位的值,而且仔細觀察上面的函式、方法,有些是以name作為引數的。

之所以要區分模板的關聯名稱(t)和模板的名稱(name),是因為一個關聯名稱t(即模板物件)上可以"包含"多個name,也就是多個模板,通過t和各自的name,可以呼叫到指定的模板

模板結構詳解

首先看Template結構:

type Template struct {
    name string
    *parse.Tree
    *common
    leftDelim  string
    rightDelim string
}

name是這個Template的名稱,Tree是解析樹,common是另一個結構,稍後解釋。leftDelim和rightDelim是左右兩邊的分隔符,預設為{{}}

這裡主要關注name和common兩個欄位,name欄位沒什麼解釋的。common是一個結構:

type common struct {
    tmpl   map[string]*Template // Map from name to defined templates.
    option option
    muFuncs    sync.RWMutex // protects parseFuncs and execFuncs
    parseFuncs FuncMap
    execFuncs  map[string]reflect.Value
}

這個結構的第一個欄位tmpl是一個Template的map結構,key為template的name,value為Template。也就是說,一個common結構中可以包含多個Template,而Template結構中又指向了一個common結構。所以,common是一個模板組,在這個模板組中的(tmpl欄位)所有Template都共享一個common(模板組),模板組中包含parseFuncs和execFuncs。

大概結構如下圖:

除了需要關注的name和common,parseFuncs和execFuncs這兩個欄位也需要了解下,它們共同成為模板的FuncMap。

New()函式和init()方法

使用template.New()函式可以建立一個空的、無解析資料的模板,同時還會建立一個common,也就是模板組

func New(name string) *Template {
    t := &Template{
        name: name,
    }
    t.init()
    return t
}

其中t為模板的關聯名稱,name為模板的名稱,t.init()表示如果模板物件t還沒有common結構,就構造一個新的common組:

func (t *Template) init() {
    if t.common == nil {
        c := new(common)
        c.tmpl = make(map[string]*Template)
        c.parseFuncs = make(FuncMap)
        c.execFuncs = make(map[string]reflect.Value)
        t.common = c
    }
}

也就是說,template.New()函式不僅建立了一個模板,還建立了一個空的common結構(模板組)。需要注意,新建立的common是空的,只有進行模板解析(Parse(),ParseFiles()等操作)之後,才會將模板新增到common的tmpl欄位(map結構)中

所以,下面的程式碼:

tmpl := template.New("mytmpl1")

執行完後將生成如下結構,其中tmpl為模板關聯名稱,mytmpl1為模板名稱。

因為還沒有進行解析操作,所以上圖使用虛線表示尚不存在的部分。

實際上,在template包中,很多涉及到操作Template的函式、方法,都會呼叫init()方法保證返回的Template都有一個有效的common結構。當然,因為init()方法中進行了判斷,對於已存在common的模板,不會新建common結構。

假設現在執行了Parse()方法,將會把模板name新增到common tmpl欄位的map結構中,其中模板name為map的key,模板為map的value。

例如:

func main() {
    t1 := template.New("test1")
    tmpl,_ := t1.Parse(
            `{{define "T1"}}ONE{{end}}
            {{define "T2"}}TWO{{end}}
            {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
            {{template "T3"}}`)
    fmt.Println(t1)
    fmt.Println(tmpl)
    fmt.Println(t1.Lookup("test1"))  // 使用關聯名稱t1檢索test1模板
    fmt.Println(t1.Lookup("T1"))
    fmt.Println(tmpl.Lookup("T2")) // 使用關聯名稱tmpl檢索T2模板
    fmt.Println(tmpl.Lookup("T3"))
}

上述程式碼的執行結果:注意前3行的結果完全一致,所有行的第二個地址完全相同。

&{test1 0xc0420a6000 0xc0420640c0  }
&{test1 0xc0420a6000 0xc0420640c0  }
&{test1 0xc0420a6000 0xc0420640c0  }
&{T1 0xc0420a6100 0xc0420640c0  }
&{T2 0xc0420a6200 0xc0420640c0  }
&{T3 0xc0420a6300 0xc0420640c0  }

首先使用template.New()函式建立了一個名為test1的模板,同時建立了一個模板組(common),它們關聯在t1變數上。

然後呼叫Parse()方法,在Parse()的待解析字串中使用define又定義了3個新的模板物件,模板的name分別為T1、T2和T3,其中T1和T2巢狀在T3中,因為呼叫的是t1的Parse(),所以這3個新建立的模板都會關聯到t1上。

也就是說,現在t1上關聯了4個模板:test1、T1、T2、T3,它們全都共享同一個common。因為已經執行了Parse()解析操作,這個Parse()會將test1、T1、T2、T3的name新增到common.tmpl的map中。也就是說,common的tmpl欄位的map結構中有4個元素。

結構如下圖:

必須注意,雖然test1、T1、T2、T3都關聯在t1上,但t1只能代表test1(所以上圖中只有test1下面標註了t1),因為t1是一個Template型別。可以認為test1、T1、T2、T3這4個模板共享一個組,但T1、T2、T3都是對外部不可見的,只能通過特殊方法的查詢找到它們。

另外,前文說過,template包中很多返回*Template的函式、方法返回的其實是原始的t(看原始碼即可知道),這個規則也適用於這裡的Parse()方法,所以tmpl和t1這兩個變數是完全等價的,都指向同一個template,即test1。所以前面的執行結果中前3行完全一致。

再回頭看上面程式碼的執行結果,假設結果中的每一行都分為3列,第一列為template name,第二個欄位為parseTree的地址,第三列為common結構的地址。因為tmpl1、t1都指向test1模板,所以前3行結果完全一致。因為test1、T1、T2、T3共享同一個common,所以第三列全都相同。因為每個模板的解析樹不一樣,所以第二列全都不一樣。

New()方法

除了template.New()函式,還有一個Template.New()方法:

// New allocates a new, undefined template associated with the given one and with the same
// delimiters. The association, which is transitive, allows one template to
// invoke another with a {{template}} action.
func (t *Template) New(name string) *Template {
    t.init()
    nt := &Template{
        name:       name,
        common:     t.common,
        leftDelim:  t.leftDelim,
        rightDelim: t.rightDelim,
    }
    return nt
}

看註釋很難理解,但是看它的程式碼,結合前文的解釋,New()方法的作用很明顯。

首先t.init()保證有一個有效的common結構,然後構造一個新的Template物件nt,這個nt除了name和解析樹parse.Tree欄位之外,其它所有內容都和t完全一致。換句話說,nt和t共享了common。

也就是說,New()方法使得名為name的nt模板物件加入到了關聯組中。更通俗一點,通過呼叫t.New()方法,可以建立一個新的名為name的模板物件,並將此物件加入到t模板組中

這和New()函式的作用基本是一致的,只不過New()函式是構建新的模板物件並構建一個新的common結構,而New()方法則是構建一個新的模板物件,並加入到已有的common結構中。

只是還是要說明,因為New()出來的新物件在執行解析之前(如Parse()),它們暫時都還不會加入到common組中,在New()出來之後,僅僅只是讓它指向已有的一個common結構。

所以:

t1 := template.New("test1")
t1 = t1.Parse(...)
t2 := t1.New("test2")
t2 = t2.Parse(...)
t3 := t1.New("test3")

結構圖:

如果t1和t2的Parse()中,都定義一個或多個name相同的模板會如何?例如:

t1 := template.New("test1")
t2 := t1.New("test2")
t1, _ = t1.Parse(
    `{{define "T1"}}ONE{{end}}
    {{define "T2"}}TWO{{end}}
    {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
    {{template "T3"}}`)
t2, _ = t2.Parse(
    `{{define "T4"}}ONE{{end}}
    {{define "T2"}}TWOO{{end}}
    {{define "T3"}}{{template "T4"}} {{template "T2"}}{{end}}
    {{template "T3"}}`)

    _ = t1.Execute(os.Stdout, "a")
    _ = t2.Execute(os.Stdout, "a")

在上面的t1和t2中,它們共享同一個common,且t1.Parse()中定義了T1、T2和T3,t2.Parse()中定義了T4、T2和T3,且兩個T2的解析內容不一樣(解析樹不一樣)。

因為T1、T2、T3、T4都會加入到t1和t2共享的common中,所以無論是通過t1還是通過t2這兩個關聯名稱都能找到T1、T2、T3、T4。但是後解析的會覆蓋先解析的,也就是說,無論是t1.Lookup("T2")還是t2.Lookup("T2")得到的T2對應的template,都是在t2.Parse()中定義的。當t1.Execute()的時候,會得到t2中定義的T2的值。

ONE TWOO
ONE TWOO

Parse()

Parse(string)方法用於解析給定的文字內容string。用法上很簡單,前面也已經用過幾次了,沒什麼可解釋的。重點在於它的作用。

當建立了一個模板物件後,會有一個與之關聯的common(如果不存在,template包中的各種函式、方法都會因為呼叫init()方法而保證common的存在)。只有在Parse()之後,才會將相關的template name放進common中,表示這個模板已經可用了,或者稱為已經定義了(defined),可用被Execute()或ExecuteTemplate(),也表示可用使用Lookup()和DefinedTemplates()來檢索模板。另外,呼叫了Parse()解析後,會將給定的FuncMap中的函式新增到common的FuncMap中,只有新增到common的函式,才可以在模板中使用。

Parse()方法是解析字串的,且只解析New()出來的模板物件。如果想要解析檔案中的內容,見後文ParseFiles()、ParseGlob()。

Lookup()、DefinedTemplates()和Templates()方法

這三個方法都用於檢索已經定義的模板,Lookup()根據template name來檢索並返回對應的template,DefinedTemplates()則是返回所有已定義的templates。Templates()和DefinedTemplates()類似,但是它返回的是[]*Template,也就是已定義的template的slice。

前面多次說過,只有在解析之後,模板才加入到common結構中,才算是已經定義,才能被檢索或執行。

當檢索不存在的templates時,Lookup()將返回nil。當common中沒有模板,DefinedTemplates()將返回空字串"",Templates()將返回空的slice。

func main() {
    t1 := template.New("test1")
    t2 := t1.New("test2")
    t1, _ = t1.Parse(
        `{{define "T1"}}ONE{{end}}
        {{define "T2"}}TWO{{end}}
        {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)
    t2, _ = t2.Parse(
        `{{define "T4"}}ONE{{end}}
        {{define "T2"}}TWOO{{end}}
        {{define "T3"}}{{template "T4"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)

    fmt.Println(t1.DefinedTemplates())
    fmt.Println(t2.DefinedTemplates())
    fmt.Println(t2.Templates())
}

返回結果:

; defined templates are: "T1", "T2", "T3", "test1", "T4", "test2"
; defined templates are: "test1", "T4", "test2", "T1", "T2", "T3"
[0xc04201c280 0xc042064100 0xc04201c1c0 0xc04201c2c0 0xc04201c300 0xc042064080]

從結果可見,返回的順序雖然不一致,但包含的template name是完全一致的。

Clone()方法

Clone()方法用於克隆一個完全一樣的模板,包括common結構也會完全克隆

t1 := template.New("test1")
t1 = t1.Parse(...)
t2 := t1.New("test2")
t2 = t2.Parse(...)

t3, err := t1.Clone()
if err != nil {
    panic(err)
}

這裡的t3和t1在內容上完全一致,但在記憶體中它們是兩個不同的物件。但無論如何,目前t3中會包含t1和t2共享的common,即使t2中定義了{{define "Tx"}}...{{end}},這個Tx也會包含在t3中。

因為是不同的物件,所以修改t3,不會影響t1/t2。

看下面的例子:

func main() {
    t1 := template.New("test1")
    t2 := t1.New("test2")
    t1, _ = t1.Parse(
        `{{define "T1"}}ONE{{end}}
        {{define "T2"}}TWO{{end}}
        {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)
    t2, _ = t2.Parse(
        `{{define "T4"}}ONE{{end}}
        {{define "T2"}}TWOO{{end}}
        {{define "T3"}}{{template "T4"}} {{template "T2"}}{{end}}
        {{template "T3"}}`)

    t3, err := t1.Clone()
    if err != nil {
        panic(err)
    }

    // 結果完全一致
    fmt.Println(t1.Lookup("T4"))
    fmt.Println(t3.Lookup("T4"))
    
    // 修改t3
    t3,_ = t3.Parse(`{{define "T4"}}one{{end}}`)
    // 結果將不一致
    fmt.Println(t1.Lookup("T4"))
    fmt.Println(t3.Lookup("T4"))
}

Must()函式

正常情況下,很多函式、方法都返回兩個值,一個是想要返回的值,一個是err資訊。template包中的函式、方法也一樣如此。

但有時候不想要err資訊,而是直接取第一個返回值,並賦值給變數。操作大概是這樣的:

t1 := template.New("ttt")
t1,err := t1.Parse(...)
if err != nil {
    panic(err)
}
...

Must()函式將上面的過程封裝了,使得Must()可以簡化上面的操作:

func Must(t *Template, err error) *Template {
    if err != nil {
        panic(err)
    }
    return t
}

當某個返回*Template,err的函式、方法需要直接使用時,可用將其包裝在Must()中,它會自動在有err的時候panic,無錯的時候只返回其中的*Template

這在賦值給變數的時候非常簡便,例如:

var t = template.Must(template.New("name").Parse("text"))

ParseFiles()和ParseGlob()

Parse()只能解析字串,要解析檔案中的內容,需要使用ParseFiles()或ParseGlob()。

template包中有ParseFiles()和ParseGlob()函式,也有ParseFiles()和ParseGlob()方法。

這兩個函式和這兩個方法的區別,看一下文件就很清晰:

$ go doc template.ParseFiles
func ParseFiles(filenames ...string) (*Template, error)
    ParseFiles creates a new Template and parses the template definitions from
    the named files. The returned template's name will have the (base) name and
    (parsed) contents of the first file. There must be at least one file. If an
    error occurs, parsing stops and the returned *Template is nil.

$ go doc template.template.ParseFiles
func (t *Template) ParseFiles(filenames ...string) (*Template, error)
    ParseFiles parses the named files and associates the resulting templates
    with t. If an error occurs, parsing stops and the returned template is nil;
    otherwise it is t. There must be at least one file.

解釋很清晰。ParseFiles()函式是直接解析一個或多個檔案的內容,並返回第一個檔名的basename作為Template的名稱,也就是說這些檔案的template全都關聯到第一個檔案的basename上。ParseFiles()方法則是解析一個或多個檔案的內容,並將這些內容關聯到t上。

看示例就一目瞭然。

例如,當前go程式的目錄下有3個檔案:a.cnf、b.cnf和c.cnf,它們的內容無所謂,反正空內容也可以解析。

func main() {
    t1,err := template.ParseFiles("a.cnf","b.cnf","c.cnf")
    if err != nil {
        panic(err)
    }
    fmt.Println(t1.DefinedTemplates())
    fmt.Println()
    fmt.Println(t1)
    fmt.Println(t1.Lookup("a.cnf"))
    fmt.Println(t1.Lookup("b.cnf"))
    fmt.Println(t1.Lookup("c.cnf"))
}

輸出結果:

; defined templates are: "a.cnf", "b.cnf", "c.cnf"

&{a.cnf 0xc0420ae000 0xc042064140  }
&{a.cnf 0xc0420ae000 0xc042064140  }
&{b.cnf 0xc0420bc000 0xc042064140  }
&{c.cnf 0xc0420bc100 0xc042064140  }

從結果中可以看到,已定義的template name都是檔案的basename,且t1和a.cnf這個template是完全一致的,即t1是檔案列表中的第一個模板物件。

結構如下圖:

理解了ParseFiles()函式,理解ParseFiles()方法、ParseGlob()函式、ParseGlob()方法,應該不會再有什麼問題。但是還是有需要注意的地方:

func main() {
    t1 := template.New("test")
    t1,err := t1.ParseFiles("a.cnf","b.cnf","c.cnf")
    if err != nil {
        panic(err)
    }
    // 先註釋下面這行
    //t1.Parse("")
    fmt.Println(t1.DefinedTemplates())
    fmt.Println()
    fmt.Println(t1)
    fmt.Println(t1.Lookup("a.cnf"))
    fmt.Println(t1.Lookup("b.cnf"))
    fmt.Println(t1.Lookup("c.cnf"))
}

執行結果:

; defined templates are: "a.cnf", "b.cnf", "c.cnf"

&{test <nil> 0xc0420640c0  }
&{a.cnf 0xc0420b0000 0xc0420640c0  }
&{b.cnf 0xc0420be000 0xc0420640c0  }
&{c.cnf 0xc0420be100 0xc0420640c0  }

發現template.New()函式建立的模板物件test並沒有包含到common中。為什麼?

因為t.ParseFiles()、t.ParseGlob()方法的解析過程是獨立於t之外的,它們只解析檔案內容,不解析字串。而New()出來的模板,需要Parse()方法來解析才會加入到common中。

將上面的註釋行取消掉,執行結果將如下:

; defined templates are: "a.cnf", "b.cnf", "c.cnf", "test"

&{test 0xc0420bc200 0xc0420640c0  }
&{a.cnf 0xc0420ae000 0xc0420640c0  }
&{b.cnf 0xc0420bc000 0xc0420640c0  }
&{c.cnf 0xc0420bc100 0xc0420640c0  }

具體原因可分析parseFiles()原始碼:

func parseFiles(t *Template, filenames ...string) (*Template, error) {
    if len(filenames) == 0 {
        // Not really a problem, but be consistent.
        return nil, fmt.Errorf("template: no files named in call to ParseFiles")
    }
    for _, filename := range filenames {
        b, err := ioutil.ReadFile(filename)
        if err != nil {
            return nil, err
        }
        s := string(b)

        // name為檔名的basename部分
        name := filepath.Base(filename)

        var tmpl *Template
        if t == nil {
            t = New(name)
        }
        // 如果呼叫t.Parsefiles(),則t.Name不為空
        // name也就不等於t.Name
        // 於是新New(name)一個模板物件給tmpl
        if name == t.Name() {
            tmpl = t
        } else {
            tmpl = t.New(name)
        }
        // 解析tmpl。如果選中了上面的else分支,則和t無關
        _, err = tmpl.Parse(s)
        if err != nil {
            return nil, err
        }
    }
    return t, nil
}

Execute()和ExecuteTemplate()

這兩個方法都可以用來應用已經解析好的模板,應用表示對需要評估的資料進行操作,並和無需評估資料進行合併,然後輸出到io.Writer中:

func (t *Template) Execute(wr io.Writer, data interface{}) error
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

兩者的區別在於Execute()是應用整個common中已定義的模板物件,而ExecuteTemplate()可以選擇common中某個已定義的模板進行應用。

例如:

func main() {
    t1 := template.New("test1")
    t1, _ = t1.Parse(`{{define "T1"}}ONE{{end}}
        {{- define "T2"}}TWO{{end}}
        {{- define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{- template "T3"}}`)
    
    _ = t1.Execute(os.Stdout,"")
    fmt.Println()
    fmt.Println("-------------")
    _ = t1.ExecuteTemplate(os.Stdout, "T2", "")
}

輸出結果:

ONE TWO
-------------
TWO

FuncMap和Funcs()

template內建了一系列函式,但這些函式畢竟有限,可能無法滿足特殊的需求。template允許我們定義自己的函式,新增到common中,然後就可以在待解析的內容中像使用內建函式一樣使用自定義的函式。

自定義函式的優先順序高於內建的函式優先順序,即先檢索自定義函式,再檢索內建函式。也就是說,如果自定義函式的函式名和內建函式名相同,則內建函式將失效。

本文只對此稍作解釋,本文的重點不是template的具體語法和用法。

在common結構中,有一個欄位是FuncMap型別的:

type common struct {
    tmpl   map[string]*Template
    option option
    muFuncs    sync.RWMutex // protects parseFuncs and execFuncs
    parseFuncs FuncMap
    execFuncs  map[string]reflect.Value
}

這個型別的定義為:

type FuncMap map[string]interface{}

它是一個map結構,key為模板中可以使用的函式名,value為函式物件(為了方便稱呼,這裡直接成為函式)。函式必須只有1個值或2個值,如果有兩個值,第二個值必須是error型別的,當執行函式時err不為空,則執行自動停止。

函式可以有多個引數。假如函式str有兩個引數,在待解析的內容中呼叫函式str時,如果呼叫方式為{{str . "aaa"}},表示第一個引數為當前物件,第二個引數為字串"aaa"。

假如,要定義一個將字串轉換為大寫的函式,可以:

import "strings"
func upper(str string) string {
    return strings.ToUpper(str)
}

然後將其新增到FuncMap結構中,並將此函式命名為"strupper",以後在待解析的內容中就可以呼叫"strupper"函式。

funcMap := template.FuncMap{
    "strupper": upper,
}

或者,直接將匿名函式放在FuncMap內部:

funcMap := template.FuncMap{
    "strupper": func(str string) string { return strings.ToUpper(str) },
}

現在只是定義了一個FuncMap例項,這個例項中有一個函式。還沒有將它關聯到模板,嚴格地說還沒有將其放進common結構。要將其放進common結構,呼叫Funcs()方法(其實呼叫此方法也沒有將其放進common,只有在解析的時候才會放進common):

func (t *Template) Funcs(funcMap FuncMap) *Template

例如:

funcMap := template.FuncMap{
    "strupper": func(str string) string { return strings.ToUpper(str) },
}
t1 := template.New("test")
t1 = t1.Funcs(funcMap)

這樣,和t1共享common的所有模板都可以呼叫"strupper"函式。

注意,必須在解析之前呼叫Funcs()方法,在解析的時候會將函式放進common結構。

下面是完整的示例程式碼:

package main

import (
    "os"
    "strings"
    "text/template"
)

func main() {
    funcMap := template.FuncMap{
        "strupper": upper,
    }
    t1 := template.New("test1")
    tmpl, err := t1.Funcs(funcMap).Parse(`{{strupper .}}`)
    if err != nil {
        panic(err)
    }
    _ = tmpl.Execute(os.Stdout, "go programming")
}

func upper(str string) string {
    return strings.ToUpper(str)
}

上面呼叫了{{strupper .}},這裡的strupper是我們自定義的函式,"."是它的引數(注意,引數不是放進括號裡)。這裡的"."代表當前作用域內的當前物件,對於這個示例來說,當前物件就是那段字串物件"go programming"。