如何在 fmt.sprintf 中拆分长行?

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

how to split long lines for fmt.sprintf

问题

我有一个非常长的 fmt.Sprintf 行。在代码中如何拆分它?我不想将所有内容放在一行中,以免代码看起来很丑陋。

fmt.Sprintf("a:%s, b:%s  ...... 这里变得非常长")

你可以将长字符串拆分成多行,以提高代码的可读性。在 Go 语言中,你可以使用字符串拼接操作符 "+" 来连接多个字符串。例如:

fmt.Sprintf("a:%s, b:%s" +
    " ...... 这里变得非常长")

这样,你可以将长字符串分成多行,使代码更易读。

英文:

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

答案1

得分: 21

使用字符串拼接来在多行上构建单个字符串值:

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

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

您可以使用原始字符串字面量将字符串拆分为包含换行符的部分:

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

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

答案2

得分: 7

你也可以在反引号内使用原始字符串字面量,像这样:

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

这种方法有一些注意事项:

  1. 原始字符串不解析转义序列
  2. 所有的空白字符都会被保留,所以在这个查询中,在FROM子句之前会有一个换行符和几个制表符。

这些问题可能对一些人来说是一个挑战,而且空白字符会产生一些丑陋的结果字符串。然而,我更喜欢这种方法,因为它允许你将长而复杂的SQL查询从代码中复制粘贴到其他上下文中,比如用于测试的SQL工作表。

英文:

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.

答案3

得分: 2

由于您已经在使用Sprintf(这意味着您将得到一个带有占位符的字符串,例如“这是一个带有%s占位符的字符串”),您可以在字符串中添加更多的占位符,然后将您想要的值放在它们自己的行上,例如:

fmt.Sprintf("这个%s非常长,我需要%s%s%s来填充其他三个字符串",
    "字符串",
    "一些非常长的语句,我不想在50行上输入",
    "另一个",
    "还有一个"
)

另一种选择是使用字符串拼接,例如"字符串1" + "字符串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".

答案4

得分: 2

另一个选项是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

英文:

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

答案5

得分: 0

为什么不将它们拆分开来:

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

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

fmt.Sprintf("this goes really long")

或者你可以像MuffinTop所示的那样使用加号将它们拆分开来。

英文:

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.

huangapple
  • 本文由 发表于 2016年2月2日 07:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/35142532.html
匿名

发表评论

匿名网友

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

确定