Skip to content

text/template: missingkey=zero ignored for map[string]interface{}  #24963

@hookenz

Description

@hookenz

If the missingkey=zero option is used in text/template expansion, then the result will be <no value> where the input data is interface{} and not string or text.

In the case of a map[string]int, the value of a literal zero will be inserted. This makes sense.
For map[string]string, then an empty string is appropriate.

For map[string]interface{} it puts <no value>. Which in my opinion makes no sense. The template itself is all text. It would be most appropriate to use an empty string if it's not an integer. Or to allow defining what value to put in it's place if the key is missing

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

go version go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

Ubuntu 14.04

What did you do?

package main

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

func main() {
	data := make(map[string]interface{})
	
	text := "APP_VERSION={{.AppVersion}}"
	tmpl, err := template.New("").Option("missingkey=zero").Parse(text)
	if err != nil {
		log.Fatal(err)
	}
	
	var b bytes.Buffer
	err = tmpl.Execute(&b, data)
	if err != nil {
		fmt.Println("template.Execute failed", err)
	}
	
	fmt.Println("Template text:", text)
	fmt.Printf("Expanded: %+v\n", b.String())
}

What did you expect to see?

Template text: APP_VERSION={{.AppVersion}}
Expanded: APP_VERSION=

What did you see instead?

Template text: APP_VERSION={{.AppVersion}}
Expanded: APP_VERSION=<no value>

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions