golang Format string by key.
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
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 Format string by key.的更多相关文章
- Predicate Format String Syntax 与字面量
字面量: 字符串:单引号或双引号扩起来: %@:系统自动添加,数字则自动去除@(): 无单双引号,系统做数字处理. Single or double quoting variables (or sub ...
- 再探Java基础——String.format(String format, Object… args)的使用
最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...
- OD: Format String, SQL Injection, XSS
Format String 格式化串漏洞 考虑如下的代码: #include<stdio.h> int main() { int a=44,b=77; printf("a=%d, ...
- Python TypeError: not enough arguments for format string
今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...
- String.Format(string, arg0)中sring格式
复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数.复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成.固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱 ...
- Oracle问题之literal does not match format string
问题: oerr ora 186101861, 00000, "literal does not match format string"// *Cause: Literals i ...
- TypeError: format string
先来看一段Python代码: class Negate: def __init__(self, val): self.val = -val def __repr__(self): return str ...
- Windows下 tensorboard出现ValueError:Invalid format string
Windows下 tensorboard出现ValueError:Invalid format string错误时,是格式错误问题,解决方法参阅我的另一篇博客 https://www.jianshu. ...
- String.format(String format, Object... args)方法详解
很多次见到同事使用这个方法,同时看到https://blog.csdn.net/qq_27298687/article/details/68921934这位仁兄写的非常仔细,我也记录一下,好加深印象. ...
随机推荐
- jQuery发布1.9正式版,最后支持IE 6/7/8
jQuery 于 2013/1/15 正式发布了 1.9 版本,这个版本最值得关注的,不是又增加了什么新功能,而是它去掉了哪些东西!jQuery 1.9 删除和改动了不少过时的 API,升级后可能会导 ...
- 如何使用Hive&R从Hadoop集群中提取数据进行分析
一个简单的例子! 环境:CentOS6.5 Hadoop集群.Hive.R.RHive,具体安装及调试方法见博客内文档. 1.分析题目 --有一个用户数据样本(表名huserinfo)10万数据左右: ...
- 下拉列表控件实例 ComboBoxControl
下拉列表控件实例 书:151页 <?xml version="1.0" encoding="utf-8"?> <s:Application x ...
- sqli-labs(十六)(order by注入)
第四十六关: http://www.bubuko.com/infodetail-2481914.html 这有篇文章讲得还不错可以看下 这关是order by后面的一个注入,用报错注入和盲注都是可以的 ...
- UVA 10256 The Great Divide(点在多边形内)
The Great Divid [题目链接]The Great Divid [题目类型]点在多边形内 &题解: 蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳 ...
- MyBatis基础入门《一》环境搭建
MyBatis基础入门<一>环境搭建 参考资料链接:http://www.mybatis.org/mybatis-3/ 使用maven构建项目,STS开发工具,jdk1.8 项目结构: m ...
- unity之让obj旋转自转等操作
1.让cube沿着矩形四个点运动 using System.Collections; using System.Collections.Generic; using UnityEngine; publ ...
- [Unit Test] Unit Test Brief Introduction
Levels of Testing- Acceptance- Performance- Functional- Integration- Unit Why Unit Testing- Feedback ...
- hdu1762 树的上的查询
2015-10-07 20:44:42 题意问的是给了一颗树,然后又1000000次查询u,v,问不在树路径上的点的编号最小值,以1为根 建这颗树,然后在同一棵子树中的点子让就输出1 否则我们记录每个 ...
- Python - 3. Input and Output
from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html Input a ...