-4

I am trying to break a string literal into multiple lines to meet my linter's expectations of short lines. Following is the code right now, it passes the linter check:

return nil,
    "",
    errors.Errorf(
            `nil cursor returned when querying for transactions for block hash %s, 
page token %s and limit %d`,
            blockHash,
            pageToken,
            limit,
    )

I don't like the formatting of the string literal. It feels aesthetically wrong ;). Is there a better way to format this? Thanks!

Flimzy
  • 68,325
  • 15
  • 126
  • 165
septerr
  • 6,257
  • 9
  • 47
  • 67
  • Aside from the opinionated nature of the question, this is a duplicate of: https://stackoverflow.com/q/7933460/13860 – Flimzy Aug 24 '21 at 07:43
  • Does this answer your question? [How do you write multiline strings in Go?](https://stackoverflow.com/questions/7933460/how-do-you-write-multiline-strings-in-go) – blackgreen Aug 24 '21 at 08:57

2 Answers2

1

Use string concatenation to construct the string from shorter lines:

errors.Errorf(
        `nil cursor returned when querying for transactions` +
        ` for block hash %s, page token %s and limit %d`,
        blockHash,
        pageToken,
        limit,
 )
1

You can use string addition:

errors.Errorf(`nil cursor returned when querying for `+
`transactions for block hash %s, page token %s and limit %d`,
            blockHash,
            pageToken,
            limit,
    )
Burak Serdar
  • 37,605
  • 3
  • 27
  • 46