Skip to content

text/template: Handling of nbsp (U+00A0) character #71722

@weakcamel

Description

@weakcamel

Go version

go version go1.24.0 darwin/amd64

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/waldekm/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/waldekm/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/65/z8mpjq_j3svgcq3cf9d_6svr0000gp/T/go-build199605776=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/Users/waldekm/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/waldekm/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/Cellar/go/1.24.0/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/waldekm/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/Cellar/go/1.24.0/libexec/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I tried to compile the following example:

package main

import "text/template"
import "os"

func main() {

        type Item struct {
                Labels string
        }

        sweaters := Item{""}

        tmpl, err := template.New("test").Parse( "Disk space on one or more of filesystems on the host is low (under 20%). Path: {{ .Labels }}.")
        if err != nil { panic(err) }

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

What did you see happen?

$ go run test.go
panic: template: test:1: bad character U+00A0

goroutine 1 [running]:
main.main()
	/Users/waldekm/gotempl/test.go:15 +0x172
exit status 2

What did you expect to see?

I expected the text to be parsed, i.e.

Disk space on one or more of filesystems on the host is low (under 20%). Path: .

I had a look at https://pkg.go.dev/text/template and it reads

The input text for a template is UTF-8-encoded text in any format. "Actions"--data evaluations or control structures--are delimited by "{{" and "}}"; all text outside actions is copied to the output unchanged.

I'm quite inexperienced with Go so please forgive my possible ignorance; to me this means an nbsp (non-breaking space) character should be handled by text/template just fine?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions