template_field.go 404 B

123456789101112131415161718192021222324
  1. // template_field.go
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "text/template"
  7. )
  8. type Person struct {
  9. Name string
  10. nonExportedAgeField string
  11. }
  12. func main() {
  13. t := template.New("hello")
  14. t, _ = t.Parse("hello {{.Name}}!")
  15. p := Person{Name: "Mary", nonExportedAgeField: "31"}
  16. if err := t.Execute(os.Stdout, p); err != nil {
  17. fmt.Println("There was an error:", err.Error())
  18. }
  19. }
  20. // Output: hello Mary!