2017-07-20 00:11 阅读 447


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")


func main() {
    myString := "Hello " + "World"

What is the differences and benefits of each?

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

4条回答 默认 最新

  • 已采纳
    dongyan1491 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 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 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 {
    点赞 1 评论 复制链接分享
  • duanke1984 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 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)


    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

    点赞 评论 复制链接分享