Skip to content

text/template: new keywords (e.g. break, continue) breaks older working programs #56538

@bep

Description

@bep

E.g.

package main

import (
	"os"
	"text/template"
)

func main() {

	t := `{{ break 32 }}`

	echo := func(in any) any {
		return in
	}

	funcs := template.FuncMap{
		"break": echo,
	}

	tmpl, err := template.New("test").Funcs(funcs).Parse(t)
	if err != nil {
		panic(err)
	}

	err = tmpl.Execute(os.Stdout, nil)
	if err != nil {
		panic(err)
	}
}

Note that the above program is a little constructed; I, personally, am not concerned about break/continue. But I notice that the upcoming new return keyword (#53015) uses the same approach. And an app I'm maintaining (Hugo) does have a return template func.

Go 1.18:

go1.18 run main.go                                                              
32

Go 1.19.3:

go version && go run main.go                                                       
go version go1.19.3 darwin/arm64
panic: template: test:1: unexpected "32" in {{break}}
goroutine 1 [running]:
main.main()
	/Users/bep/dev/go/bep/temp/main.go:24 +0x258
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions