0

for example, in go I have x := map[string]bool{"1":true,"2":true}
How can I print it to go.tmpl, using the built-in template package?

// go.tmpl
{{x}}

after render, I want it to be like

x := map[string]bool{"1":true,"2":true}

Do I have some approaches?

1 Answer 1

1
s := `{{x}}`
x := map[string]bool{"1": true, "2": true}
t, err := template.New("t").Funcs(template.FuncMap{
    "x": func() string { return fmt.Sprintf("x := %#v", x) },
}).Parse(s)
if err != nil {
    panic(err)
}
if err := t.Execute(os.Stdout, nil); err != nil {
    panic(err)
}

https://play.golang.org/p/Pww7-PFIWXJ

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.