如何格式化多行字符串文字

huangapple go评论62阅读模式
英文:

How to format a multiline string literal

问题

我正在尝试将一个字符串文字分成多行,以满足我的代码检查工具对短行的期望。以下是当前的代码,它通过了代码检查:

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 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!

答案1

得分: 1

使用字符串拼接来构建较短行的字符串:

errors.Errorf(
    `在查询交易时返回了空游标` +
    `,区块哈希为 %s,页面令牌为 %s,限制为 %d`,
    blockHash,
    pageToken,
    limit,
)
英文:

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,
 )

答案2

得分: 1

你可以使用字符串拼接:

errors.Errorf(`在查询块哈希为%s、页面令牌为%s和限制为%d的交易时返回了空游标`,
            blockHash,
            pageToken,
            limit,
    )
英文:

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,
    )

huangapple
  • 本文由 发表于 2021年8月24日 09:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/68900747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定