# Named String Formatting

**URL:** <https://forum.golangbridge.org/t/named-string-formatting/3802>\
**Category:** Getting Help\
**Created:** [November 6, 2016, 6:00pm UTC](https://forum.golangbridge.org/t/named-string-formatting/3802 "2016-11-06T18:00:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadhasivam](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@sadhasivam](https://forum.golangbridge.org/u/sadhasivam)\
**Post date:** [November 6, 2016, 6:00pm UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/1 "2016-11-06T18:00:18Z")

</div>

Hi Folks,

I wanted to interpolate string and get the formatted output using named place holders. The requirement is somewhat similar to Template/Text/Html package but slightly different.

Using default template packages escapes characters and gets into a route of issues than I wanted. Further, my requirement is very simple like Taking a string with named parameters & Map of interfaces should output full string as like Python format.

Tprintf(“Hello %{Name}s %{Apos}s”, map[string]interface{} {“Name” :“GoLang”, “Apos”:"!",})

ouput:  
Hello GoLang !

---

<div class="post-metadata">

**Author:** ![christophberger](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/christophberger/32/11391_2.png) [@christophberger](https://forum.golangbridge.org/u/christophberger)\
**Post date:** [November 6, 2016, 9:01pm UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/2 "2016-11-06T21:01:47Z")

</div>

Just curious - what problem do named parameters solve?

---

<div class="post-metadata">

**Author:** ![calmh](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/calmh/32/2896_2.png) [@calmh](https://forum.golangbridge.org/u/calmh)\
**Post date:** [November 7, 2016, 5:35am UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/4 "2016-11-07T05:35:49Z")

</div>

You could iterate over the map and do a string replacement for each key found.

Assuming you only support `%s`: [https://play.golang.org/p/kn24JK87VI](https://play.golang.org/p/kn24JK87VI)

You could of course also parse out the format character and pass that to Sprintf.

---

<div class="post-metadata">

**Author:** ![sadhasivam](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@sadhasivam](https://forum.golangbridge.org/u/sadhasivam)\
**Post date:** [November 7, 2016, 2:13pm UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/5 "2016-11-07T14:13:11Z")

</div>

Well, Golang is amazing by design & structure.  
Since fmt.Sprintf handles both

1. DataType based operations  
fmt.Sprintf(“String Data : %s , Int Data: %d”, “string”, 100)
2. Indexed Data Operations.  
fmt.Sprintf(“String Data : %[1]s , Int Data: %[2]d”, map)  
would’d be nice if Golang has Named data Interpolation for a given string template.some of like this.

[https://play.golang.org/p/COHKlB2RML](https://play.golang.org/p/COHKlB2RML)

---

<div class="post-metadata">

**Author:** ![calmh](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/calmh/32/2896_2.png) [@calmh](https://forum.golangbridge.org/u/calmh)\
**Post date:** [November 7, 2016, 2:22pm UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/6 "2016-11-07T14:22:59Z")

</div>

We have two implementations of Tprintf in this thread already, both only a handful of lines long, so it doesn’t seem to be necessary to have it in the standard library. 🙂

---

<div class="post-metadata">

**Author:** ![sadhasivam](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@sadhasivam](https://forum.golangbridge.org/u/sadhasivam)\
**Post date:** [November 7, 2016, 2:29pm UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/7 "2016-11-07T14:29:55Z")

</div>

Agreed Naming of the function could be different. but if something function solving the same purpose idea please share i can make use of it.

---

<div class="post-metadata">

**Author:** ![benjamin\_thomas](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/benjamin_thomas/32/1311_2.png) [@benjamin\_thomas](https://forum.golangbridge.org/u/benjamin_thomas)\
**Post date:** [November 15, 2016, 6:06am UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/8 "2016-11-15T06:06:08Z")

</div>

Nah, don’t agree. Lack of named parameters is definitely a pain. The problem it solves? Not worrying about parameter position on the right when iterating on the left. Text template is the only way to go if you have too many variables to deal with, but can be overkill.

Both implementations are not flexible enough to be really useful IMHO.

My 2 cents of course.

---

<div class="post-metadata">

**Author:** ![Rio\_M](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/rio_m/32/1214_2.png) [@Rio\_M](https://forum.golangbridge.org/u/Rio_M)\
**Post date:** [November 15, 2016, 10:16am UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/9 "2016-11-15T10:16:29Z")

</div>

Template solution is very similar:

```
m := map[string]interface{}{"name": "John", "age": 47}
t := template.Must(template.New("").Parse("Hi {{.name}}. Your age is {{.age}}\n"))
t.Execute(os.Stdout, m)
```

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [February 13, 2017, 10:16am UTC](https://forum.golangbridge.org/t/named-string-formatting/3802/10 "2017-02-13T10:16:56Z")

</div>

Closed on February 13, 2017, 10:16am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
