多行字符串文字的缩进样式

多行字符串文字的缩进样式

问题描述:

What is the proposed style for indenting a raw string literal? If I indent it based on its first line, it might not align properly in editors that have a different tab length. For example:

if select == nil {
    select, err = db.Prepare(`select name
                              from table
                              where id=$1`)
    if err != nil {
        return nil, err
    }
}

I have found this question, but I am still unclear: Best practice for long string literals in Go

Should I do it like below?

if select == nil {
    select, err = db.Prepare(`
select name
from table
where id=$1`)
    if err != nil {
        return nil, err
    }
}

缩进原始字符串文字的建议样式是什么? 如果我根据第一行缩进,则它在具有不同制表符长度的编辑器中可能无法正确对齐。 例如: p>

 如果select == nil {
 select,err = db.Prepare(`se​​lect name 
 from table 
 where id = $ 1`)
 如果err!= nil {
 return nil,err 
} 
} 
  code>  pre> 
 
 

我已经找到了这个问题,但是我仍然不清楚: Go中长字符串文字的最佳做法 p> \ n

我应该像下面那样吗? p>

 如果select == nil {
 select,err = db.Prepare(`
select name 
from table 
where  id = $ 1`)
 if err!= nil {
 return nil,err 
} 
} 
  code>  pre> 
  div>

Considering both propositions would add newline or spaces to the litteral string, I would favor (even though fmt format the first line):

    select, err = db.Prepare(
     `select name
from table
where id=$1`)

As the OP akonsu comments below, it seems consistent with the style of the golang code itself, as seen in src/cmd/go/main.go#L175, which keeps the first line at the level of the opening '('

var usageTemplate = `Go is a tool for managing Go source code.
Usage:
go command [arguments]
...
`