@aliasliyu4
2017-03-05T19:54:28.000000Z
字数 2670
阅读 1944
大部分的编程语言都是库实现模版,也是为了巩固mvc模式下服务端的统治地位,模版是如此的重要它甚至能够检验一个语言的成功和失败。然后,如今的现状是,单页应用大行其道,模版的使用已经变的越来越少。可能比较牢不可破的场景还是在生产邮件实体的时候。这篇文章中,我会使用一个例子去介绍如何用golang的template包生成基于定义在文件中的模版文件生成邮件实体。尽力去展现这个包中最常用的一些特性。
golang 代码:
package main
import (
"fmt"
"html/template"
"os"
"time"
)
type Account struct {
FirstName string
LastName string
}
type Purchase struct {
Date time.Time
Description string
AmountInCents int
}
type Statement struct {
FromDate time.Time
ToDate time.Time
Account Account
Purchases []Purchase
}
func formatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d/%d/%d", day, month, year)
}
func formatAsDollars(valueInCents int) (string, error) {
dollars := valueInCents / 100
cents := valueInCents % 100
return fmt.Sprintf("$%d.%2d", dollars, cents), nil
}
func urgentNote(acc Account) string {
return fmt.Sprintf("You have earned 100 vip points that can be used for purchases")
}
func hello() string {
return fmt.Sprintf("hello word!")
}
func createMockStatement() Statement {
return Statement{
FromDate: time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC),
ToDate: time.Date(2017, 2, 1, 0, 0, 0, 0, time.UTC),
Account: Account{
FirstName: "alias",
LastName: "liyu4",
},
Purchases: []Purchase{
Purchase{
Date: time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "dell ultrasharp 24 monitor",
AmountInCents: 23026,
},
Purchase{
Date: time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "macbook air",
AmountInCents: 88821,
},
},
}
}
func main() {
fmap := template.FuncMap{
"formatAsDollars": formatAsDollars,
"formatAsDate": formatAsDate,
"urgentNote": urgentNote,
"hello": hello,
}
t, err := template.New("email.tmpl").Funcs(fmap).ParseFiles("email.tmpl")
if err != nil {
panic(err)
}
//Execute applies a parsed template to the specified data object,
//writing the output to wr. If an error occurs executing the template or writing its output,
//execution stops, but partial results may already have been written to the output writer.
//A template may be executed safely in parallel.
if err := t.Execute(os.Stdout, createMockStatement()); nil != err {
panic(err)
}
}
这段代码中最重要的部分是数据结构和main函数,其他的函数唯一的目的是展示从模版中调用不同的办法。
模版文件 email.tmpl:
{{with .Account -}}
Dear {{.FirstName}} {{.LastName}},
{{- end}}
Below are your account statement details for period from {{.FromDate | formatAsDate}} to {{.ToDate | formatAsDate}}.
{{if .Purchases -}}
Your purchases:
{{- range .Purchases }}
{{ .Date | formatAsDate}} {{ printf "%-20s" .Description }} {{.AmountInCents | formatAsDollars -}}
{{- end}}
{{- else}}
You didn't make any purchases during the period.
{{- end}}
{{$note := urgentNote .Account -}}
{{if $note -}}
Note: {{$note}}
{{- end}}
Best Wishes,
Customer Service
输出:
Dear alias liyu4,
Below are your account statement details for period from 1/1/2017 to 1/2/2017.
Your purchases:
1/1/2017 dell ultrasharp 24 monitor $230.26
1/1/2017 macbook air $888.21
Note: You have earned 100 vip points that can be used for purchases
Best Wishes,
Customer Service
原文: http://goinbigdata.com/example-of-using-templates-in-golang/