Interpreted string literals in Go

huangapple go评论80阅读模式
英文:

Interpreted string literals in Go

问题

通常在日志消息或标准错误消息中编写长而详细的字符串是很好的。Python使用逗号分隔的字符串字面值来处理这个问题,例如:

log.warn("The operation failed on the %d iteration. "
         "Resumed on the %d iteration.",
         failed, resumed)

Go似乎有一种使用反引号的原始字符串字面值的解决方案,但我找不到任何关于解释字符串字面值的样式指南。我是不是漏掉了什么,或者除了使用变量之外没有其他选择?例如:

msg := fmt.Sprintf("The operation failed on the %d iteration. ", failed)
msg += fmt.Sprintf("Resumed on the %d iteration.", resumed)
log.println(msg)
英文:

It's frequently nice to write long, informative strings for log messages or stderr messages. Python handles this with comma separated string literals, like:

log.warn("The operation failed on the %d iteration. "
         "Resumed on the %d iteration.",
         failed, resumed)

Go appears to have a solution for raw string literals, by using back quotes but I can't find any style guide for interpreted string literals. Am I missing something or is there no option but to use a variable? E.g.

msg := fmt.Sprintf("The operation failed on the %d iteration. ", failed)
msg += fmt.Sprintf("Resumed on the %d iteration.", resumed)
log.println(msg)

答案1

得分: 2

你可以直接使用+符号:

fmt.Printf("The operation failed on the %d iteration. "+
	"Resumed on the %d iteration.",
	failed, resumed,
)

Playground

在标准库中有一些使用+符号的示例,其中大部分在测试中使用。示例。更多示例请参考以下搜索结果:http://golang.org/search?q=%22%2B\n

英文:

You could just use +:

fmt.Printf("The operation failed on the %d iteration. "+
	"Resumed on the %d iteration.",
	failed, resumed,
)

Playground.

There are examples in the standard library of using + for that, most of them in tests. Example. For more examples see this search request: http://golang.org/search?q=%22%2B\n.

1: http://golang.org/search?q=%22%5C%2B%5Cn "examples"

答案2

得分: 0

首先,我不明白你的Python示例如何工作。这里有一个类似的示例:

import logging
logging.warn("foo %d", "bar %d", 1, 2)

会导致以下错误:

TypeError: %d format: a number is required, not str

其次,在Go语言中,你有几个选项:

多行字符串:

msg := fmt.Sprintf(`The operation failed on the %d iteration. 
		    Resumed on the %d iteration.`, 2, 3)
log.Println(msg)

但这将导致多行消息。

另一个选项:

log.Println(fmt.Sprintf("The operation failed on the %d iteration. ", failed), 
            fmt.Sprintf("Resumed on the %d iteration.", resumed))

这种方式看起来更好,而且可能比字符串拼接更快。

英文:

First of all, I don't see how your python example would even work. Here's something similar:

>>> import logging
>>> logging.warn("foo %d", "bar %d", 1,2)

causes:

 TypeError: %d format: a number is required, not str

Second, in Go, you have a few options:

Multi-line strings:

msg := fmt.Sprintf(`The operation failed on the %d iteration. 
		    Resumed on the %d iteration.`, 2, 3)
log.Println(msg)

But this will result in a multiline message.

Another option:

  log.Println(fmt.Sprintf("The operation failed on the %d iteration. ", failed), 
              fmt.Sprintf("Resumed on the %d iteration.", resumed))

which both looks better and will probably be faster than string concatenation.

huangapple
  • 本文由 发表于 2014年10月6日 18:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/26214643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定