template_validation.go 555 B

1234567891011121314151617181920212223
  1. // template_validation.go
  2. package main
  3. import (
  4. "text/template"
  5. "fmt"
  6. )
  7. func main() {
  8. tOk := template.New("ok")
  9. //a valid template, so no panic with Must:
  10. template.Must(tOk.Parse("/* and a comment */ some static text: {{ .Name }}"))
  11. fmt.Println("The first one parsed OK.")
  12. fmt.Println("The next one ought to fail.")
  13. tErr := template.New("error_template")
  14. template.Must(tErr.Parse(" some static text {{ .Name }"))
  15. }
  16. /* Output:
  17. The first one parsed OK.
  18. The next one ought to fail.
  19. panic: template: error_template:1: unexpected "}" in command
  20. */