2

Is there a way to write a verb in a format string that outputs nothing, even though some values are supplied to Printf/Sprintf/Fprintf? In other words, is there a value of s for which this code outputs nothing?

fmt.Printf(s, 42)

In C, the answer is easy: the empty string, because the superfluous value is ignored. In Go, an empty format string leads to the output %!(EXTRA int=42). Which I consider a good thing, because the format string is most likely wrong. Python even throws "TypeError: not all arguments converted during string formatting" in that situation. But if we truly want to gobble up, ignore, skip, cancel the values, how could we convince Printf of our good intentions?

If the value is a string, it's easy: truncate to zero:

    fmt.Printf("nothing%.0s here", "foul")

You could even omit the 0 in that verb, I realised after digging up a bash version of my question. But what if the value is a number?

If you wonder, why not simply omit superfluous values: the context is that only the format string can be touched, being the parameter of a function. The function body feeds the format some value(s) of predetermined types, such as in this simplified playground example. Usually, the format passed in wants to represent the value, but sometimes you may want to ignore it. Of course the function could grow up and accept an interface instead of a format string, but I'm just wondering if Printf could pull it off on its own like it can in C.

Community
  • 1
  • 1
Stein
  • 1,558
  • 23
  • 30
  • 2
    See [How to ignore extra fields for fmt.Sprintf](http://stackoverflow.com/questions/35043202/how-to-ignore-extra-fields-for-fmt-sprintf), which uses `fmt.Sprintf()`, but is similar. It tries to count the verbs and truncate the arguments with a slice. It also argues against using dynamic format strings. – e0k Dec 18 '16 at 00:27
  • karmakaze's answer inspired my to try fmt's explicit argument indexes. If you refer to one value by index, then fmt no longer complains about extra values. So if you supply multiple values to printf and require that format strings represent at least one of them, then the format string can refer to that mandatory value by index (e.g. `%[1]d`) and doesn't have to worry about the other values. – Stein Dec 18 '16 at 14:51

1 Answers1

2

This may not be exactly what you're looking for but it's worth looking at the text/template package. Then you could define a template with Parse("{{.Name}} items are made of {{.Material}}") and pass in to Execute a map[string]string containing keys "Name" and "Material". This has the added advantage that the format string can also arrange the output order of values.

karmakaze
  • 34,689
  • 1
  • 30
  • 32
  • A better alternative to format crossed my mind but I thought I'd stick to a simple question to begin with. But this sure is one I didn't know. I worked it down to [this playground example](https://play.golang.org/p/jppr5Ew-xj). The struct FooTalk can be inlined away, but I'd prefer callers to have a clear view of the interface they can access, even though there is no static type checking. – Stein Dec 18 '16 at 14:37