多行字符串字面值的缩进风格

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

indentation style for multi-line string literals

问题

原始字符串字面值的缩进风格有多种方式。如果根据其第一行进行缩进,可能在具有不同制表符长度的编辑器中无法正确对齐。例如:

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

我找到了这个问题,但我仍然不清楚:https://stackoverflow.com/questions/20940194/best-practice-for-long-string-literals-in-go

我应该像下面这样做吗?

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

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: https://stackoverflow.com/questions/20940194/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
    }
}

答案1

得分: 5

你可能对github.com/lithammer/dedent包感兴趣,它提供了类似于Python标准库中的textwrap.dedent()的功能。

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

You may be interrested by github.com/lithammer/dedent package which provide something similar to Python std textwrap.dedent().

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

答案2

得分: 3

考虑到两个命题都会在字符串中添加换行符或空格,我更倾向于以下写法(尽管fmt格式化第一行):

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

正如OP akonsu在下面的评论中所提到的,这似乎与golang代码本身的风格一致,就像在src/cmd/go/main.go#L175中所看到的那样,它将第一行保持在开头的(的级别上。

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

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]
...
`

答案3

得分: 0

关于SQL,空格并不重要,所以这只是个人偏好。在我的情况下,由于SQL是一种与Go不同的语言,我喜欢确保SQL代码与Go代码不共用一行:

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

Regarding SQL, the whitespace doesn't matter, so it's just a personal preference. In my case, as SQL is a different language from Go, I like to make sure that the SQL code shares no lines with Go code:

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

huangapple
  • 本文由 发表于 2015年1月23日 23:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/28113427.html
匿名

发表评论

匿名网友

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

确定