dongshuobei1037 2018-01-05 18:43
浏览 239
已采纳

在Golang中将字符串放在两个%之间

I have a string in Golang called mystring and I want to put it between 2 percentages (%mystring%) however until now I wasn't able to do it.

The things that I have tried are:

value := fmt.Sprintf("%%s%",mystring)
value := fmt.Sprintf("%s%s%s","%",mystring,"%")
value := fmt.Sprintf("/% %s/%",mystring)

But when I print it, in the end I receive a nil. Example: the value of mystring is "HelloWorld" then I get: "%HelloWorld%nil"

Right now I am receiving this as result:

/%s/%!(NOVERB)%!(EXTRA string=HelloWorld)<nil>

So, what am I missing? Thanks

  • 写回答

2条回答 默认 最新

  • dongtangyi8962 2018-01-05 18:45
    关注

    You need to escape the %'s in format string using another %:

    value := fmt.Sprintf("%%%s%%",mystring)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?