doujiyong7604 2016-04-10 17:15
浏览 50
已采纳

字符串中允许多个参数[Go]

I'm using Gomail to grab data from a form and email it to myself. If I wanted to get the users full name, this is what I would use:

m.SetBody("text/html", fmt.Sprintf("<b>Full Name</b>: %s", msg.completeName))

It something like this in the email:

Full Name: John Michael Smith

Now if I wanted to add a message field to the code

m.SetBody("text/html", fmt.Sprintf("<b>Full Name</b>: %s", msg.completeName, "<br> <b>Message</b> %s", msg.Content))

It outputs this:

Full Name: John Michael Smith%!(EXTRA string=

Message: %s, string=Hi there!.)

I want it to look like this:

Full Name: John Michael Smith

Message: Hi there!

  • 写回答

1条回答 默认 最新

  • duang5049 2016-04-10 17:27
    关注

    The issue is that you're using Sprintf the wrong way.

    The Sprintf needs a string format as first argument and then all the variables you need to be inserted in the final string.

    Thus your code should be:

    m.SetBody("text/html", fmt.Sprintf("<b>Full Name</b>: %s <br><b>Message</b> %s", msg.completeName, msg.Content))
    

    For more informations I suggest you to read the Sprintf documentation

    NOTE: In the comment I said "why don't you concatenate the string?" since you can also do:

    m.SetBody("text/html", "<b>Full Name</b>: "+ msg.completeName +" <br><b>Message</b> " + msg.Content))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码