<h2>用<code>strings.Replacer</code></h2>
<p>使用<a href="https://golang.org/pkg/strings/#Replacer" rel="noreferrer">^{<cd1>}</a>,实现愿望的格式化程序非常简单和紧凑。</p>
<pre><code>func main() {
file, err := "/data/test.txt", "file not found"
log("File {file} had error {error}", "{file}", file, "{error}", err)
}
func log(format string, args ...string) {
r := strings.NewReplacer(args...)
fmt.Println(r.Replace(format))
}
</code></pre>
<p>输出(在<a href="https://play.golang.org/p/sDVi-3NUiw" rel="noreferrer">Go Playground</a>上尝试):</p>
<pre><code>File /data/test.txt had error file not found
</code></pre>
<p>我们可以通过在<code>log()</code>函数中自动将括号添加到参数名中,使其更易于使用:</p>
<pre><code>func main() {
file, err := "/data/test.txt", "file not found"
log2("File {file} had error {error}", "file", file, "error", err)
}
func log2(format string, args ...string) {
for i, v := range args {
if i%2 == 0 {
args[i] = "{" + v + "}"
}
}
r := strings.NewReplacer(args...)
fmt.Println(r.Replace(format))
}
</code></pre>
<p>输出(在<a href="https://play.golang.org/p/3Odo_LmM6c" rel="noreferrer">Go Playground</a>上尝试):</p>
<pre><code>File /data/test.txt had error file not found
</code></pre>
<p>是的,您可以说它只接受<code>string</code>参数值。这是真的。如果再提高一点,就不会是这样了:</p>
<pre><code>func main() {
file, err := "/data/test.txt", 666
log3("File {file} had error {error}", "file", file, "error", err)
}
func log3(format string, args ...interface{}) {
args2 := make([]string, len(args))
for i, v := range args {
if i%2 == 0 {
args2[i] = fmt.Sprintf("{%v}", v)
} else {
args2[i] = fmt.Sprint(v)
}
}
r := strings.NewReplacer(args2...)
fmt.Println(r.Replace(format))
}
</code></pre>
<p>输出(在<a href="https://play.golang.org/p/0KtRjDIFTN" rel="noreferrer">Go Playground</a>上尝试):</p>
<pre><code>File /data/test.txt had error 666
</code></pre>
<p>接受参数作为<code>map[string]interface{}</code>并返回结果作为<code>string</code>的一种变体:</p>
<pre><code>type P map[string]interface{}
func main() {
file, err := "/data/test.txt", 666
s := log33("File {file} had error {error}", P{"file": file, "error": err})
fmt.Println(s)
}
func log33(format string, p P) string {
args, i := make([]string, len(p)*2), 0
for k, v := range p {
args[i] = "{" + k + "}"
args[i+1] = fmt.Sprint(v)
i += 2
}
return strings.NewReplacer(args...).Replace(format)
}
</code></pre>
<p>在<a href="https://play.golang.org/p/i9XUTN9V3m" rel="noreferrer">Go Playground</a>上试试。</p>
<h2>与<code>text/template</code></h2>
<p>你的模板解决方案或建议也太冗长了。它可以这样写(省略错误检查):</p>
<pre><code>type P map[string]interface{}
func main() {
file, err := "/data/test.txt", 666
log4("File {{.file}} has error {{.error}}", P{"file": file, "error": err})
}
func log4(format string, p P) {
t := template.Must(template.New("").Parse(format))
t.Execute(os.Stdout, p)
}
</code></pre>
<p>输出(在<a href="https://play.golang.org/p/D-hoAeCakq" rel="noreferrer">Go Playground</a>上尝试):</p>
<pre><code>File /data/test.txt has error 666
</code></pre>
<p>如果要返回<code>string</code>(而不是打印到标准输出),可以这样做(在<a href="https://play.golang.org/p/L9L9OerQYZ" rel="noreferrer">Go Playground</a>上尝试):</p>
<pre><code>func log5(format string, p P) string {
b := &bytes.Buffer{}
template.Must(template.New("").Parse(format)).Execute(b, p)
return b.String()
}
</code></pre>
<h2>使用显式参数索引</h2>
<p>这已经在另一个答案中提到了,但是要完成它,要知道同一个显式参数索引可以被任意次数使用,从而导致同一个参数被多次替换。在这个问题中阅读更多关于这个的信息:<a href="https://stackoverflow.com/questions/37001449/replace-all-variables-in-sprintf-with-same-variable">Replace all variables in Sprintf with same variable</a></p>