I wish Go has string interpolation 😮‍💨
Is there a variant of `fmt.Sprint` that doesn't add spaces between operands?
PS: I hate `fmt.Sprintf`
#go #golang

@drsensor There is strings.Builder if you only want to accumulate strings, but other than that, Sprintf is your only option in the base library, I think. Unless you want to completely crazy and use text/template.

Dunno if there's anything third-party that would fit your bill.

@klausman thanks!
I guess it's better to write my own function using `strings.Builder`
```go
func S(strs ...str) {
var b strings.Builder
for _, s := range strs {
b.WriteString(s)
}
return b.String()
}
```
with this snippet as a baseline, I can modify them to support my own custom type

@drsensor @klausman

There are strings.Join and slices.Concat which behave like the function that you just wrote.

And for the original question, you don't have to have spaces between placeholders:

fmt.Sprintf("%d%d", 2, 3)

Go Playground - The Go Programming Language

@Merovius ugh, seems I misread the examples in the standard docs. Thanks for pointing out.
Now I'm confused on what's the meaning of “Spaces are added between operands when neither is a string.” 🤔

@drsensor Oh, "fun". This demonstrates: https://go.dev/play/p/NdoiBDbQuzn

TBH I never use Sprint with more than one argument, so I wasn't really aware either.

Go Playground - The Go Programming Language

@Merovius oh no, I began to hate Go https://go.dev/play/p/h5mjpM3qJB1
seriously, what's the purpose of Sprint 😂
Go Playground - The Go Programming Language

@drsensor @Merovius fmt.Sprint() and fmt.Sprintf() are very different beasts.

Only fmt.Sprint() does the "spaces" added thing. The idea being that if you go:

a := 1
b := 2
fmt.Sprint(a, b)

conveniently assumes you want to see:

1 2

rather than

12

If you go:

fmt.Sprint(a, " gt ", b)

Sprint() assumes you've considered spacing and prints:

1 gt 2

fmt.Sprint() is purely a convenience function which mostly "does what you want". If you don't like it, use the myriad prescriptive functions.

@markd @Merovius at first I assume Sprint always add spaces, but no, it only add spaces at certain condition. Due to this weird behaviour, I'm wondering if there is an example when it's convenient to use Sprint instead of others 🤔

@drsensor @Merovius I see Sprint() as more of a diagnostic/debug output service.

a := 1
b := 2
Sprint(a, b)

is less keystrokes and less thinking than

Sprintf("%d %d", a, b)

as is:

Sprint(a, " < ", b)

vs
Sprintf("%d < %d", a, b)

In general, as a diagnostic print, you can just feed arguments to Sprint() and you'll get good output without worrying about formatting, e.g:

Sprint(int, string, []byte, float, struct)

just works.

So yeah, convenient. Just not a strict formatting tool.

@drsensor does Go have a printf like function that can interpolate with the classic %s format?