having a problem understanding text/template printf behaviour:
the following code originates from
https://golang.org/pkg/text/template/#example_Template_funcminimal changes I made include:
added "tottile" function
alter output2 > Output 2: {{printf "%s%s%s" . . .| title | totitle | title }}
added > output 3: {{title . }}{{totitle . }}{{title . }}
--------
program is below with output results.
why should not output 2 match output 3?
if they should match is this a known bug ?
paul
------------------
package main
import (
"log"
"os"
"strings"
"text/template"
)
func main() {
// First we create a FuncMap with which to register the function.
funcMap := template.FuncMap{
// The name "title" is what the function will be called in the template text.
"title": strings.Title,
"totitle": strings.ToTitle,
}
// A simple template definition to test our function.
// We print the input text several ways:
// - the original
// - title-cased
// - title-cased and then printed with %q
// - printed with %q and then title-cased.
const templateText = `
Input: {{printf "%q" .}}
Output 2: {{printf "1:%q 2:%q 3:%q" . . .| title | totitle | title }}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%s%s%s" . . .| title | totitle | title }}
output 3: {{title . }}{{totitle . }}{{title . }}
`
// Create a template, add the function map, and parse the text.
tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
if err != nil {
log.Fatalf("parsing: %s", err)
}
// Run the template to verify the output.
err = tmpl.Execute(os.Stdout, "the go programming language")
if err != nil {
log.Fatalf("execution: %s", err)
}
}
program results:
Input: "the go programming language"
Output 2: 1:"THE GO PROGRAMMING LANGUAGE" 2:"THE GO PROGRAMMING LANGUAGE" 3:"THE GO PROGRAMMING LANGUAGE"
Output 0: The Go Programming Language
Output 1: "The Go Programming Language"
Output 2: THE GO PROGRAMMING LANGUAGETHE GO PROGRAMMING LANGUAGETHE GO PROGRAMMING LANGUAGE
output 3: The Go Programming LanguageTHE GO PROGRAMMING LANGUAGEThe Go Programming Language
Program exited.
-------------