Skip to content

text/template: Unexpected assignment #56490

@jmooring

Description

@jmooring

What version of Go are you using (go version)?

go version go1.19.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jmooring/.cache/go-build"
GOENV="/home/jmooring/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jmooring/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jmooring/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org"
GOROOT="/snap/go/current"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/current/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jmooring/code/go-sandbox/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3041838949=/tmp/go-build -gno-record-gcc-switches"

What did you do?

This works as expected (stdout is 3):

import (
	"os"
	"text/template"
)

func main() {

	a := [3]int{1, 2, 3}

	t := `
		{{- $x := 0 }}
		{{- $i := 0 }}
		{{- range $i = . }}
		{{- end }}
		{{- $i }}
	`

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

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

Now, reverse the order of the first two lines of the template:

import (
	"os"
	"text/template"
)

func main() {

	a := [3]int{1, 2, 3}

	t := `
		{{- $i := 0 }}
		{{- $x := 0 }}
		{{- range $i = . }}
		{{- end }}
		{{- $i }}
	`

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

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

Stdout is [1 2 3], but it should be 3.

Same issue with html/template.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions