-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
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>