doujiao3016 2017-11-30 10:04
浏览 110


I need to create a string with html link inside.

fmt.Sprint("<a href=\"%s\">%s</a>", "/myUrl", "link text");

Expected result is

<a href="/myUrl">link text</a>

But real result is

<a href="%s">%s</a>/myUrllink text

What am I doing wrong?

By the way, I have this warning in GoLand

enter image description here

  • 写回答

2条回答 默认 最新

  • doujiao1981 2017-11-30 10:09

    fmt.Sprint will print the string as it is. If you want to format the string, you should use fmt.Sprintf.

    The SO community can be very harsh on beginners, if people do not like what you have written or how you worded the question you will get down voted. Don't fool yourself thinking people are here to help, they only care for the reputation.

    Luckily in Go we have pretty good docs. If you ever think something is not working properly, chances are that a glance at the docs website will be enough to get you going.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • douchenbiao0916 2017-11-30 14:30

    func Sprintf

    func Sprintf(format string, a ...interface{}) string

    Sprintf formats according to a format specifier and returns the resulting string.

    func Sprint

    func Sprint(a ...interface{}) string

    Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

    Also fmt is pretty big and very useful standard package with a good documentation. You can find a lot of different I/O functions there.

    Also it is perkily and weird to write "incorrect result" if you hasn't read a documentation.




  • ¥15 求苹果推信imessage批量推信技术
  • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
  • ¥15 利用加权最小二乘法求亚马逊各类商品的价格指标?怎么求?
  • ¥15 c++ word自动化,为什么可用接口是空的?
  • ¥15 Matlab计算100000*100000的矩阵运算问题:
  • ¥50 VB6.0如何识别粘连的不规则的数字图片验证码
  • ¥16 需要完整的这份订单所有的代码,可以加钱
  • ¥15 Stata数据分析请教
  • ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
  • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料