Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreted string literals in Go

It's frequently nice to write long, informative strings for log messages or stderr messages. Python handles this with comma separated string literals, like:

log.warn("The operation failed on the %d iteration. "
         "Resumed on the %d iteration.",
         failed, resumed)

Go appears to have a solution for raw string literals, by using back quotes but I can't find any style guide for interpreted string literals. Am I missing something or is there no option but to use a variable? E.g.

msg := fmt.Sprintf("The operation failed on the %d iteration. ", failed)
msg += fmt.Sprintf("Resumed on the %d iteration.", resumed)
log.println(msg)
like image 757
Steve Crook Avatar asked Feb 26 '26 22:02

Steve Crook


1 Answers

You could just use +:

fmt.Printf("The operation failed on the %d iteration. "+
    "Resumed on the %d iteration.",
    failed, resumed,
)

Playground.

There are examples in the standard library of using + for that, most of them in tests. Example. For more examples see this search request: http://golang.org/search?q=%22\%2B\n.

like image 159
Ainar-G Avatar answered Feb 28 '26 18:02

Ainar-G



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!