Best practice for long string literals in Go

别来无恙 提交于 2019-12-23 06:48:23

问题


I've got a long string literal in 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?


回答1:


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.




回答2:


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




回答3:


You could do:

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

db.Exec(s)



回答4:


Since we're talking about SQL in this instance…

It is rare to pass string literals as column values in an INSERT or UPDATE. 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")



回答5:


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.



来源:https://stackoverflow.com/questions/20940194/best-practice-for-long-string-literals-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!