dqd54099
2018-04-13 22:40 阅读 1.1k
已采纳

如何在golang中向字符串变量添加变量

I'm trying to add a value to a variable string in golang, without use printf because I'm using revel framework and this is for a web enviroment instead of console, this is the case:

data := 14
response := `Variable string content`

so I can't get variable data inside variable response, like this

response := `Variable string 14 content`

Any idea?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dqzuo0327 dqzuo0327 2018-04-13 22:56

    Why not use fmt.Sprintf?

    data := 14
    response := fmt.Sprintf("Variable string %d content", data)
    
    点赞 评论 复制链接分享
  • duanpo7796 duanpo7796 2019-07-27 18:32

    If you want to keep the string in variable rather than to print out, try like this:

    data := 14
    response := "Variable string" + data + "content"
    
    点赞 评论 复制链接分享

相关推荐