dongse7261 2014-01-05 22:43
浏览 310

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

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?

  • 写回答

4条回答 默认 最新

  • doukongpao0903 2014-01-08 10:00
    关注

    You could do:

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向