Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Strings in GoLang?

i've been researching for a while now but could not find anything.

For Example

var name = "tom"
sentence := "My Name is #{name}"

In JS this works with ${var} and in ruby with #{var}

So are there Template Strings in GoLang ?

Thanks for your help

like image 572
Ben Ammann Avatar asked Dec 05 '25 10:12

Ben Ammann


2 Answers

The Solution is

sentence := fmt.Sprintf("My Name is %s", name)
like image 187
Ben Ammann Avatar answered Dec 08 '25 01:12

Ben Ammann


The better way is using template. It allows you to specify a name.

sentence := "My Name is {{ .name }}"
templ := template.Must(template.New("myname").Parse(sentence))
templ.Execute(os.Stdout, map[string]interface{}{
    "name": "tom",
})
like image 29
Jannchie Avatar answered Dec 08 '25 00:12

Jannchie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!