简体   繁体   English

Go 中长字符串文字的最佳实践

[英]Best practice for long string literals in Go

I've got a long string literal in Go:我在 Go 中有一个长字符串文字:

db.Exec("UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')")

I see two ways to make this more manageable: raw quotes, or multiple concatenated quotes:我看到有两种方法可以使这更易于管理:原始引号或多个串联引号:

db.Exec(`UPDATE mytable SET (I, Have, Lots, Of, Fields) 
         = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 
            'wowsolong', 'loooooooooooooooooooooooooong')`)

db.Exec("UPDATE mytable SET (I, Have, Lots, Of, Fields) = " + 
    "('suchalongvalue', 'thisislongaswell', 'ohmansolong', " +
    "'wowsolong', 'loooooooooooooooooooooooooong')")

The first feels more right, but the preceding spaces will be included in the string, making the resulting string have awkward runs of spaces in it.第一个感觉更正确,但前面的空格将包含在字符串中,使生成的字符串中包含尴尬的空格。 Is either of these considered idiomatic Go?这些中的任何一个都被认为是惯用的围棋吗?

It looks weird putting the long string literal in the parameter like that.像这样将长字符串文字放在参数中看起来很奇怪。 I would prefer:我更喜欢:

const updateQuery=`
UPDATE mytable SET (I, Have, Lots, Of, Fields) 
= ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 
'wowsolong', 'loooooooooooooooooooooooooong')`

func doUpdate(){
  db.Exec(updateQuery)
}

I also prefer a single newline at the beginning to the odd spaces in each line.我也更喜欢开头的单个换行符而不是每行中的奇数空格。 That way you can kill it with strings.Trim if it causes problems.这样你就可以用strings.Trim杀死它,如果它引起问题。

This is what I do:这就是我所做的:

q := `UPDATE mytable SET (I, Have, Lots, Of, Fields) = ` +
     `('suchalongvalue', ` + 
     `'thisislongaswell', ` +
     `'wowsolong', ` + 
     `loooooooooooooooooooooooooong')`

db.Exec(q)

I think it looks a lot cleaner我觉得它看起来干净多了

You could do:你可以这样做:

s := `UPDATE mytable SET (I, Have, Lots, Of, Fields) = `
s += `('suchalongvalue', `
s += `'thisislongaswell', `
s += `'wowsolong', `
s += `loooooooooooooooooooooooooong')`

db.Exec(s)

Since we're talking about SQL in this instance…因为我们在这个例子中谈论的是 SQL…

It is rare to pass string literals as column values in an INSERT or UPDATE .INSERTUPDATE中将字符串文字作为列值传递是很少见的。 You'll almost always be passing in computed values from code, in which case it's far better to use parameterized queries.您几乎总是从代码中传递计算值,在这种情况下,使用参数化查询要好得多。 In the rare case where you do know a value at compile time, parameterization is still generally the right answer:在极少数情况下,您确实在编译时知道一个值,参数化通常仍然是正确的答案:

_, err := db.Exec(
    `UPDATE mytable SET (I, Have, Lots, Of, Fields) = ($1, $2, $3, $4, $5)`,
    "suchalongvalue",
    "thisislongaswell",
    "ohmansolong",
    "wowsolong",
    "loooooooooooooooooooooooooong")

I prefer:我更喜欢:

var updateStatement = `
    UPDATE
        mytable
    SET
        I = 'suchalongvalue'
        ,Have = 'thisislongaswell'
        ,Lots = 'ohmansolong'
        ,Of = 'wowsolong'
        ,Fields = 'loooooooooooooooooooooooooong'
`
func update(updateStatement string) {
    db.Exec(updateStatement)
}

Should looks much more cleaner.应该看起来更干净。 At least thats been taught to me.至少那是教给我的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM