用 strings.Replacer
使用
strings.Replacer,实现您所需的格式化程序非常容易且紧凑。
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))}输出(在Go Playground上尝试):
File /data/test.txt had error file not found
通过在
log()函数中自动在参数名称中添加方括号,可以使使用起来更加愉快:
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))}输出(在Go Playground上尝试):
File /data/test.txt had error file not found
是的,您可以说这仅接受
string参数值。这是真的。有了更多的改进,这将不是真的:
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))}输出(在Go Playground上尝试):
File /data/test.txt had error 666
此方法的一种变体,接受params作为a
map[string]interface{}并以a形式返回结果string:
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)}在Go Playground上尝试一下。
用 text/template
您的模板解决方案或建议也太冗长。可以这样紧凑地编写(省略错误检查):
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)}输出(在Go Playground上尝试):
File /data/test.txt has error 666
如果要返回
string(而不是将其打印到标准输出),则可以这样做(在Go
Playground上尝试):
func log5(format string, p P) string { b := &bytes.Buffer{} template.Must(template.New("").Parse(format)).Execute(b, p) return b.String()}使用显式参数索引
在另一个答案中已经提到了这一点,但是要完成此操作,请知道相同的显式参数索引可以任意使用多次,从而导致同一参数被多次替换。



