example:

$ go get github.com/hoisie/mustache

 package main

 import (
"github.com/hoisie/mustache" "bytes"
"fmt"
"strings"
"text/template"
) func FormatByKey(f string, m map[string]interface{}) (string, error) {
var tpl bytes.Buffer
t := template.Must(template.New("").Parse(f))
if err := t.Execute(&tpl, m); err != nil {
return f, err
}
return tpl.String(), nil
} func main() {
// use mustache
m1 := map[string]interface{}{"age": "", "c": "world"}
data := mustache.Render("hello {{c}}, I'm {{age}} years old.\n", m1)
println(data) // use golang native template
m := map[string]interface{}{"name": "John", "age": }
s := "I'm {{.name}}, I'm {{.age}} years old.Hi {{.name}}. Your age is {{.age}}\n"
fmt.Println(FormatByKey(s, m)) //strings.NewReplacer
ss := []string{"{{.name}}", "John", "{{.age}}", ""}
r := strings.NewReplacer(ss...)
fmt.Println(r.Replace(s))
}

$ go run test.go

another example.

 package main

 import (
"fmt"
"strings"
) func main() {
Tprintf("Hello %{Name}s %{Apos}s", map[string]interface{}{"Name": "GoLang", "Apos": "!"})
} func Tprintf(format string, params map[string]interface{}) {
for key, val := range params {
format = strings.Replace(format, "%{"+key+"}s", fmt.Sprintf("%s", val), -)
}
fmt.Printf(format)
}

Named String Formatting

Golang Template is powerful

golang Format string by key.的更多相关文章

  1. Predicate Format String Syntax 与字面量

    字面量: 字符串:单引号或双引号扩起来: %@:系统自动添加,数字则自动去除@(): 无单双引号,系统做数字处理. Single or double quoting variables (or sub ...

  2. 再探Java基础——String.format(String format, Object… args)的使用

    最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...

  3. OD: Format String, SQL Injection, XSS

    Format String 格式化串漏洞 考虑如下的代码: #include<stdio.h> int main() { int a=44,b=77; printf("a=%d, ...

  4. Python TypeError: not enough arguments for format string

    今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...

  5. String.Format(string, arg0)中sring格式

    复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数.复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成.固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱 ...

  6. Oracle问题之literal does not match format string

    问题: oerr ora 186101861, 00000, "literal does not match format string"// *Cause: Literals i ...

  7. TypeError: format string

    先来看一段Python代码: class Negate: def __init__(self, val): self.val = -val def __repr__(self): return str ...

  8. Windows下 tensorboard出现ValueError:Invalid format string

    Windows下 tensorboard出现ValueError:Invalid format string错误时,是格式错误问题,解决方法参阅我的另一篇博客 https://www.jianshu. ...

  9. String.format(String format, Object... args)方法详解

    很多次见到同事使用这个方法,同时看到https://blog.csdn.net/qq_27298687/article/details/68921934这位仁兄写的非常仔细,我也记录一下,好加深印象. ...

随机推荐

  1. lambda 表达式拼接

    类库: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...

  2. GO language

    看到有人说GO是未来10年的主流了,不论是速度迅速接近于C,还是语法简洁接近于C,结果尽然还是编译型的,不需要虚拟机,生成程序已经是本地字节码. 得,我不淡定了,这个不学,枉为程序员啊. 今天,讲讲l ...

  3. 解决Windows内存问题的两个小工具RamMap和VMMap

    解决Windows内存问题需要对操作系统的深入理解,同时对于如何运用Windows调试器或性能监控器要有工作认知.如果你正试着得到细节,诸如内核堆栈大小或硬盘内存消耗,你会需要调试器命令和内核数据架构 ...

  4. java基础(三) -基本数据类型

    变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过定义不同类型的变量,可以在内存 ...

  5. MyBatis基础入门《一》环境搭建

    MyBatis基础入门<一>环境搭建 参考资料链接:http://www.mybatis.org/mybatis-3/ 使用maven构建项目,STS开发工具,jdk1.8 项目结构: m ...

  6. hbase shell operate

    , start hdfs [hadoop@alamps sbin]$ ./start-all.sh This script is Deprecated. Instead use start-dfs.s ...

  7. Android -- ViewGroup源码分析+自定义

    1,我们前三篇博客了解了一下自定义View的基本方法和流程 从源码的角度一步步打造自己的TextView 深入了解自定义属性 onMeasure()源码分析 之前,我们只是学习过自定义View,其实自 ...

  8. 解决sqlserver修改被阻止的提示

    https://jingyan.baidu.com/article/f79b7cb3664f299144023ef8.html 工具——选项——选择designers---表设计器和数据库设计器,将阻 ...

  9. Sitecore 9有什么新功能

    在这个新版本中有很多值得爱的东西.每个人都会有自己喜欢的新功能,但是,我想与你分享一些地雷: xConnect 正如我们在Sitecore的官方Sitecore 9新闻稿中所见的那样,“新的xConn ...

  10. mac下编译cpu only caffe并用xCode建caffe工程

    mac编译caffe 好像又变容易了,直接git clone下载blvc源码,make.config里去掉了CPU_ONLY前面的注释,并没有安装任何依赖,也可能是自己mac上本来有, xCode里调 ...