how to split long lines for fmt.sprintf
Asked Answered
N

5

14

I have a very long line in fmt.Sprintf. How do I split it in the code? I don't want to put everything in a single line so the code looks ugly.

fmt.Sprintf("a:%s, b:%s  ...... this goes really long")
Newmint answered 1/2, 2016 at 23:41 Comment(0)
F
21

Use string concatenation to construct a single string value on multiple lines:

 fmt.Sprintf("a:%s, b:%s " +
    " ...... this goes really long",
    s1, s2)

The long string in this example is built at compile time because the string concatenation is a constant expression.

You can split the string at contained newlines using a raw string literal:

     fmt.Sprintf(`this text is on the first line
and this text is on the second line,
and third`)
Frijol answered 1/2, 2016 at 23:44 Comment(0)
S
8

You can also use raw string literals inside backticks, like this:

columns := "id, name"
table := "users"
query := fmt.Sprintf(`
    SELECT %s
    FROM %s
  `, columns, table)
fmt.Println(query)

There are a few caveats to this approach:

  1. Raw strings don't parse escape sequences
  2. All the whitespace will be preserved, so there will be a newline and then several tabs before the FROM clause in this query.

These problems can be a challenge for some, and the whitespace will produce some ugly resulting strings. However, I prefer this approach as it allows you to copy and paste long, complex SQL queries outside of your code and into other contexts, like sql worksheets for testing.

Schild answered 4/4, 2018 at 19:54 Comment(0)
G
2

Since you're using Sprintf already (meaning you'll have a string like "this is the string with %s placeholders in it") you could just add more place holders to the string and then put the values you'd like there on their own lines like;

fmt.Sprintf("This %s is so long that I need %s%s%s for the other three strings,
"string",
"some super long statement that I don't want to type on 50 lines",
"another one of those",
"yet another one of those")

Another option is just to use string concatenation like "string 1" + "string 2".

Grill answered 1/2, 2016 at 23:51 Comment(2)
Seems like it would be better to concatenate the strings so the long string is built at compile time instead of at run time as the above would do. (Unless that's optimized somehow.)Phyllys
@AndySchweig I'm not sure what optimizations the Go compiler makes however there is no reason this couldn't be optimized the same as concatenation. The same rules apply, if all values are known at compile time then the calculation can be done, if one or more values isn't known until runtime then it cannot.Grill
H
2

Another option is strings.Builder:

package main

import (
   "fmt"
   "strings"
)

func main() {
   b := new(strings.Builder)
   fmt.Fprint(b, "North")
   fmt.Fprint(b, "South")
   println(b.String() == "NorthSouth")
}

https://golang.org/pkg/strings#Builder

Huckleberry answered 16/4, 2021 at 1:26 Comment(0)
A
0

Why don't you split it out:

fmt.Sprintf("a:%s, b:%s ", x1, x2)

fmt.Sprintf("...... ")

fmt.Sprintf("this goes really long")

Or you can split them out with the plus sign as indicated by MuffinTop.

Acidimetry answered 1/2, 2016 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.