Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indentation style for multi-line string literals

Tags:

go

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
    }
}
like image 956
akonsu Avatar asked Jan 23 '15 15:01

akonsu


Video Answer


1 Answers

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]
...
`
like image 139
VonC Avatar answered Oct 15 '22 10:10

VonC