mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
04.4.md
This commit is contained in:
23
eBook/examples/chapter_15/template_field.go
Normal file
23
eBook/examples/chapter_15/template_field.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// template_field.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
nonExportedAgeField string
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := template.New("hello")
|
||||
t, _ = t.Parse("hello {{.Name}}!")
|
||||
p := Person{Name:"Mary", nonExportedAgeField: "31"}
|
||||
if err := t.Execute(os.Stdout, p); err != nil {
|
||||
fmt.Println("There was an error:", err.Error())
|
||||
}
|
||||
}
|
||||
// Output: hello Mary!
|
Reference in New Issue
Block a user