doutan1857 2017-07-20 00:11
浏览 852
已采纳

为什么要使用fmt.Sprint?

I really don't understand the benefit of using fmt.Sprint compared to add strings together with +. Here is an example of both in use:

func main() {
    myString := fmt.Sprint("Hello", "world")
    fmt.Println(myString)
}

and

func main() {
    myString := "Hello " + "World"
    fmt.Println(myString)
}

What is the differences and benefits of each?

  • 写回答

4条回答 默认 最新

  • dongyan1491 2017-07-20 00:43
    关注

    In your example there are no real differences as you are Sprintf to simply concaternate strings. That is indeed something which can be solved more easily by using the '+' operator.

    Take the following example, where you want to print a clear error message like "Product with ID '42' could not be found.". How does that look with your bottom approach?

    productID := 42;
    myString := "Product with ID '" + productID + "' could not be found."
    

    This would give an error (mismatched types string and int), because Go does not have support for concatenate different types together.

    So you would have to transform the type to a string first.

    productID := 42
    myString := "Product with ID '" + strconv.Itoa(productID) + "' could not be found."
    

    And, this you would have to do for every single data type other than strings.

    The fmt package in Go and similar formatting packages in almost any other language solve this by helping you with the conversions and keeping your strings clear of mass '+' operators.

    Here is how the example would look like using fmt

    product := 42
    myString := fmt.Sprintf("Product with ID '%d' could not be found.", product)
    

    Here %d is the formatting verb for 'print the argument as a number'. See https://golang.org/pkg/fmt/#hdr-Printing the various other ways of printing other types.

    Compared to concatenating fmt allows you to write your strings in a clear way, separating the template/text from the variables. And, it simplifies printing data types other than strings a lot.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doufubu2518 2018-12-06 23:43
    关注

    The real difference between Printf and Sprintf is

    Sprintf formats the string and does not print to console. It returns the formatted string.

    Printf formats the string and prints in the console. It does not return the formatted string.

    func Printf(format string, a ...interface{}) (n int, err error) {
    ...
    }
    
    func Sprintf(format string, a ...interface{}) string {
    ...
    }
    
    评论
  • duanke1984 2017-07-20 00:37
    关注

    If you call a function with concatenated string as argument, you will have to evaluate argument prior to call. Then if function chooses not to act on argument (think logging when log level is lower then needed for printing), you already incurred the overhead of concatenation.

    Very similar to your example, in one case you do concatenation and in other not. With high volume of those operations it may become noticeable. Again, logging is a good example.

    In specific case of Sprint, it is not that relevant of course, but perhaps it's good to be consistent?

    评论
  • dtjwov4984 2017-07-20 07:44
    关注

    Most of the arguments have already been written, exclude one. Localization with Sprintf is much easier and has better defined roles between programmer and localizator (someone who speaks foreign language). Of course not each app really needs that. Let's choose:

    s := fmt.Sprintf(t('%s is %d and comes from %s'), name, age, place)
    

    or

    s := name + t(' is ') + strconv.Itoa(age) + t(' and comes from ') + place
    

    Translation of fragments of text is confusing. Also sprintf allows you formatting number etc

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 VB6.0中PICTUREBOX加载本地图片无法显示
  • ¥100 关于游戏app session获取的问题
  • ¥15 MYSQL数据库建表
  • ¥15 爬虫程序爬取TTGChina网站文章代码
  • ¥35 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。
  • ¥15 如何用下图方法在AMESim中搭建离心泵模型
  • ¥15 C#连接服务器,请求时报Ssl/Tsl未能建立安全通道
  • ¥15 xcode15build的c++ dylib在10.15上不兼容
  • ¥15 CPLD如何实现在线逻辑分析
  • ¥15 控制面板卸载无权限!!