Last active
July 17, 2018 14:14
-
-
Save AlphaWong/0bf930021a29e9c1309c79202bfcb4a5 to your computer and use it in GitHub Desktop.
"text/template" issue for embed string struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
"strings" | |
"text/template" | |
) | |
type myStr string | |
type myStruct struct { | |
string | |
} | |
func main() { | |
const ( | |
master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{printf "- %t\n" . }}{{end}}{{end}}` | |
overlay = `{{define "list"}} {{join . ", "}}{{end}} ` | |
) | |
var ( | |
funcs = template.FuncMap{"join": strings.Join} | |
guardians = []interface{}{myStruct{"123"}, "Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"} | |
) | |
masterTmpl, err := template.New("master").Funcs(funcs).Parse(master) | |
if err != nil { | |
log.Fatal(err) | |
} | |
overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err := masterTmpl.Execute(os.Stdout, guardians); err != nil { | |
log.Fatal(err) | |
} | |
if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil { | |
log.Fatal(err) | |
} | |
} |
A can fmt.Sprintf("%s", A)
as a string do not mean A is the type of string which A might be
type MyStruct struct{
string
}
A := MyStruct{"text"}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/XXaKg5I7npI