go - 如何拆分 fmt.sprintf 的长行

标签 go gofmt

我在 fmt.Sprintf 中有很长的一行。我如何在代码中拆分它?我不想将所有内容都放在一行中,这样代码看起来很难看。

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

最佳答案

使用string concatenation在多行上构造单个字符串值:

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

这个例子中的长字符串是在编译时构建的,因为字符串连接是一个常量表达式。

您可以使用 raw string literal 在包含的换行符处拆分字符串:

     fmt.Sprintf(`this text is on the first line
and this text is on the second line,
and third`)

关于go - 如何拆分 fmt.sprintf 的长行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142532/

相关文章:

go - 有没有办法在不完全禁用 gofmt 的情况下防止 gofmt 将代码函数转换为多行?

go - Go 中的缩进 : tabs or spaces?

bash - 在Circleci中使用golang-ci-lint

go - 如何获取 HTTP 响应的大小?

performance - Golang - go run 需要很长时间才能执行

go - 在golang html/template中对 slice 进行排序?

go - 你能调用 gofmt 来格式化你从编写它的 Go 代码内部编写的文件吗?

SublimeText 中的 Gofmt 插件未找到 GOPATH 错误

go - 使用 Goroutine 对数字进行排序时出现意外结果