多行字符串文字的缩进样式
indentation style for multi-line string literals
缩进原始字符串文字的建议样式是什么?如果我根据它的第一行缩进它,它可能无法在具有不同制表符长度的编辑器中正确对齐。例如:
if select == nil {
select, err = db.Prepare(`select name
from table
where id=`)
if err != nil {
return nil, err
}
}
我找到了这个问题,但我还是不清楚:Best practice for long string literals in Go
我应该像下面那样做吗?
if select == nil {
select, err = db.Prepare(`
select name
from table
where id=`)
if err != nil {
return nil, err
}
}
考虑到这两个命题都会在字符串中添加换行符或空格,我赞成(即使 fmt
format the first line):
select, err = db.Prepare(
`select name
from table
where id=`)
作为 OP akonsu comments below, it seems consistent with the style of the golang code itself, as seen in src/cmd/go/main.go#L175
,它使第一行保持在开头 '(
'
的水平
var usageTemplate = `Go is a tool for managing Go source code.
Usage:
go command [arguments]
...
`
关于SQL,空格无关紧要,所以这只是个人喜好。在我的例子中,由于 SQL 是与 Go 不同的语言,我想确保 SQL 代码与 Go 代码不共享任何行:
if select == nil {
select, err = db.Prepare(`
select name
from table
where id =
`)
if err != nil {
return nil, err
}
}
缩进原始字符串文字的建议样式是什么?如果我根据它的第一行缩进它,它可能无法在具有不同制表符长度的编辑器中正确对齐。例如:
if select == nil {
select, err = db.Prepare(`select name
from table
where id=`)
if err != nil {
return nil, err
}
}
我找到了这个问题,但我还是不清楚:Best practice for long string literals in Go
我应该像下面那样做吗?
if select == nil {
select, err = db.Prepare(`
select name
from table
where id=`)
if err != nil {
return nil, err
}
}
考虑到这两个命题都会在字符串中添加换行符或空格,我赞成(即使 fmt
format the first line):
select, err = db.Prepare(
`select name
from table
where id=`)
作为 OP akonsu comments below, it seems consistent with the style of the golang code itself, as seen in src/cmd/go/main.go#L175
,它使第一行保持在开头 '(
'
var usageTemplate = `Go is a tool for managing Go source code.
Usage:
go command [arguments]
...
`
关于SQL,空格无关紧要,所以这只是个人喜好。在我的例子中,由于 SQL 是与 Go 不同的语言,我想确保 SQL 代码与 Go 代码不共享任何行:
if select == nil {
select, err = db.Prepare(`
select name
from table
where id =
`)
if err != nil {
return nil, err
}
}