Skip to content

text/template: Unable to set zero value for fields in template #75543

@nitin455

Description

@nitin455

Go version

go1.24.0

Output of go env in your module/workspace:

set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=0
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\xxx\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\xxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\xxx\AppData\Local\Temp\go-build2279385750=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=NUL
set GOMODCACHE=C:\Users\xx\go\pkg\mod
set GONOPROXY=gitlab.com/xxx/*
set GONOSUMDB=gitlab.com/xxx/*
set GOOS=windows
set GOPATH=C:\Users\xxx\go
set GOPRIVATE=gitlab.com/xxx/*
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\xxx\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.24.0
set GOWORK=
set PKG_CONFIG=pkg-config

What did you do?

I am trying to parse template using text/template package. My input payload has few fields and which are optional in certain cases. So i was trying to set them with their zero value when missing in input payload.

To achieve this, I took help of Options and set missingkey=zero option while parsing template. The expected behavior was to set empty string value for missing fields with string data type but it was showing <no value>

Below is the code snippet I used to test this issue.

package main

import (
	"bytes"
	"fmt"
	"strings"
	"text/template"
)

func main() {
	payload := map[string]interface{}{
		//"PackagerType" is intentionally missing
		"Name": "Checkout",
	}

	tmpl, err := template.New("test").
		//Option("missingkey=zero").
		Parse(`Name:         {{.Name}}
PackagerType: {{.PackagerType}}
UnknownKey:   {{.DoesNotExist}}
`)
	if err != nil {
		panic(err)
	}

	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, payload); err != nil {
		panic(err)
	}

	fmt.Println("OUTPUT:")
	fmt.Println(buf.String())
}

What did you see happen?

I used feature of Options to set missingkey=zero option while parsing template. The expected behavior was to set empty string value for missing fields with string data type but it was showing <no value>

Sample output:

$ go run main.go
OUTPUT:
Name:         Checkout
PackagerType: <no value>
UnknownKey:   <no value>

What did you expect to see?

I was expecting below output to be displayed.

OUTPUT:
Name:         Checkout
PackagerType: 
UnknownKey:   

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