我在fmt.Sprintf中有一个很长的行.如何在代码中拆分它?我不想把所有东西放在一行,所以代码看起来很难看.
fmt.Sprintf("a:%s, b:%s ...... this goes really long")
Run Code Online (Sandbox Code Playgroud)
Cer*_*món 13
使用字符串连接在多行上构造单个字符串值:
fmt.Sprintf("a:%s, b:%s " +
" ...... this goes really long",
s1, s2)
Run Code Online (Sandbox Code Playgroud)
fmt.Sprintf(`this text is on the first line
and this text is on the second line,
and third`)
Run Code Online (Sandbox Code Playgroud)
您还可以在反引号内使用原始字符串文字,如下所示:
columns := "id, name"
table := "users"
query := fmt.Sprintf(`
SELECT %s
FROM %s
`, columns, table)
fmt.Println(query)
Run Code Online (Sandbox Code Playgroud)
这种方法有一些注意事项:
FROM在此查询中的子句之前将有一个换行符和几个制表符。这些问题对于某些人来说可能是一个挑战,并且空格会产生一些难看的结果字符串。然而,我更喜欢这种方法,因为它允许您将长而复杂的 SQL 查询复制并粘贴到代码之外并粘贴到其他上下文中,例如用于测试的 SQL 工作表。
| 归档时间: |
|
| 查看次数: |
3814 次 |
| 最近记录: |