template_variables.go 468 B

123456789101112131415161718192021222324
  1. // template_variables.go
  2. package main
  3. import (
  4. "os"
  5. "text/template"
  6. )
  7. func main() {
  8. t := template.New("test")
  9. t = template.Must(t.Parse("{{with $3 := `hello`}}{{$3}}{{end}}!\n"))
  10. t.Execute(os.Stdout, nil)
  11. t = template.Must(t.Parse("{{with $x3 := `hola`}}{{$x3}}{{end}}!\n"))
  12. t.Execute(os.Stdout, nil)
  13. t = template.Must(t.Parse("{{with $x_1 := `hey`}}{{$x_1}} {{.}} {{$x_1}}{{end}}!\n"))
  14. t.Execute(os.Stdout, nil)
  15. }
  16. /* Output:
  17. hello!
  18. hola!
  19. hey hey hey!
  20. */