青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Fork me on GitHub
隨筆 - 215  文章 - 13  trackbacks - 0
<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


專注即時通訊及網游服務端編程
------------------------------------
Openresty 官方模塊
Openresty 標準模塊(Opm)
Openresty 三方模塊
------------------------------------
本博收藏大部分文章為轉載,并在文章開頭給出了原文出處,如有再轉,敬請保留相關信息,這是大家對原創作者勞動成果的自覺尊重!!如為您帶來不便,請于本博下留言,謝謝配合。

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

相冊

Awesome

Blog

Book

GitHub

Link

搜索

  •  

積分與排名

  • 積分 - 220982
  • 排名 - 117

最新評論

閱讀排行榜

http://stackoverflow.com/questions/11123865/golang-format-a-string-without-printing

1. Simple strings

For "simple" strings (typically what fits into a line) the simplest solution is using fmt.Sprintf() and friends (fmt.Sprint(), fmt.Sprintln()). These are analog to the functions without the starter Sletter, but these Sxxx() variants return the result as a string instead of printing them to the standard output.

For example:

s := fmt.Sprintf("Hi, my name is %s and I'm %d years old.", "Bob", 23)

The variable s will be initialized with the value:

Hi, my name is Bob and I'm 23 years old.

Tip: If you just want to concatenate values of different types, you may not automatically need to use Sprintf() (which requires a format string) as Sprint() does exactly this. See this example:

i := 23
s := fmt.Sprint("[age:", i, "]") // s will be "[age:23]"

For concatenating only strings, you may also use strings.Join() where you can specify a custom separator string (to be placed between the strings to join).

Try these on the Go Playground.

2. Complex strings (documents)

If the string you're trying to create is more complex (e.g. a multi-line email message), fmt.Sprintf()becomes less readable and less efficient (especially if you have to do this many times).

For this the standard library provides the packages text/template and html/template. These packages implement data-driven templates for generating textual output. html/template is for generating HTML output safe against code injection. It provides the same interface as package text/template and should be used instead of text/template whenever the output is HTML.

Using the template packages basically requires you to provide a static template in the form of a string value (which may be originating from a file in which case you only provide the file name) which may contain static text, and actions which are processed and executed when the engine processes the template and generates the output.

You may provide parameters which are included/substituted in the static template and which may control the output generation process. Typical form of such parameters are structs and map values which may be nested.

Example:

For example let's say you want to generate email messages that look like this:

Hi [name]!

Your account is ready, your user name is: [user-name]

You have the following roles assigned:
[role#1], [role#2], ... [role#n]

To generate email message bodies like this, you could use the following static template:

const emailTmpl = `Hi {{.Name}}!

Your account is ready, your user name is: {{.UserName}}

You have the following roles assigned:
{{range $i, $r := .Roles}}{{if ne $i 0}}, {{end}}{{.}}{{end}}
`

And provide data like this for executing it:

data := map[string]interface{}{
    "Name":     "Bob",
    "UserName": "bob92",
    "Roles":    []string{"dbteam", "uiteam", "tester"},
}

Normally output of templates are written to an io.Writer, so if you want the result as a string, create and write to a bytes.Buffer (which implements io.Writer). Executing the template and getting the result as string:

t := template.Must(template.New("email").Parse(emailTmpl))
buf := &bytes.Buffer{}
if err := t.Execute(buf, data); err != nil {
    panic(err)
}
s := buf.String()

This will result in the expected output:

Hi Bob!

Your account is ready, your user name is: bob92

You have the following roles assigned:
dbteam, uiteam, tester

Try it on the Go Playground.

Note: you may also display the result of a template execution if you provide os.Stdout as the target (which also implements io.Writer):

t := template.Must(template.New("email").Parse(emailTmpl))
if err := t.Execute(os.Stdout, data); err != nil {
    panic(err)
}

This will write the result directly to os.Stdout. Try this on the Go Playground.

posted on 2017-05-02 17:12 思月行云 閱讀(273) 評論(0)  編輯 收藏 引用 所屬分類: Golang
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲视频免费在线观看| 欧美精选午夜久久久乱码6080| 久久夜色精品国产欧美乱极品| 国产精品实拍| 欧美制服第一页| 午夜精品影院| 好吊色欧美一区二区三区四区 | 国产日韩一区在线| 久久久久国产精品一区| 久久久91精品国产一区二区三区| 1000部国产精品成人观看| 久久成人精品一区二区三区| 久久久7777| 一二美女精品欧洲| 欧美一区在线看| 影音先锋亚洲精品| 亚洲精品欧美激情| 国产亚洲精品一区二区| 91久久精品国产| 欧美日韩一区二区三区在线观看免 | 亚洲免费大片| 亚洲一二三区精品| 亚洲大胆美女视频| 一本色道久久88精品综合| 久久免费视频在线| 99re66热这里只有精品3直播| 在线视频中文亚洲| 亚洲精品韩国| 欧美在线资源| 亚洲线精品一区二区三区八戒| 久久成人精品电影| 亚洲一区久久久| 嫩草成人www欧美| 久久av红桃一区二区小说| 免费在线观看一区二区| 欧美一级理论片| 一区二区三区精品| 亚洲成色www久久网站| 欧美在线国产| 国产精品久久久久91| 亚洲国产欧美不卡在线观看| 欧美色另类天堂2015| 欧美一级在线播放| 国产精品国码视频| 久久乐国产精品| 国产精品久久久久久久午夜| 欧美激情免费在线| 激情综合亚洲| 麻豆国产va免费精品高清在线| 欧美日韩综合网| 久久精品欧洲| 国产精品综合av一区二区国产馆| 亚洲精选成人| 日韩视频一区二区| 亚洲一区二区三区在线播放| 99精品视频免费观看视频| 久久久成人精品| 欧美在线一二三四区| 国产精品久久国产三级国电话系列| 91久久久久久| 国产精品久久久久影院色老大| 欧美国产专区| 亚洲国产专区| 麻豆91精品91久久久的内涵| 久久久国产一区二区| 国产欧美日韩精品在线| 一区二区三区精品视频| 99视频在线观看一区三区| 欧美黄色一区二区| 亚洲精品1区| 在线亚洲精品福利网址导航| 欧美成人精品在线观看| 亚洲福利视频免费观看| 亚洲精品一区二区三区蜜桃久| 免费观看一区| 在线看欧美视频| 性欧美精品高清| 一区二区三区国产精品| 欧美制服丝袜| 西瓜成人精品人成网站| 国产欧美精品va在线观看| 亚洲美女电影在线| 亚洲视频免费在线观看| 欧美高清视频一区二区| 亚洲国产一二三| 在线播放中文一区| 99精品欧美一区二区三区| 一区二区三区www| 欧美国产视频日韩| 99精品久久| 欧美专区日韩专区| 国产精品夜夜夜| 欧美在线91| 久久久噜久噜久久综合| 国产精品一区免费视频| 欧美一区二区三区精品电影| 久久久午夜视频| 欧美性色综合| 日韩午夜免费视频| 欧美中文在线字幕| 欧美区一区二| 亚洲欧美一区在线| 免费观看日韩| 中日韩在线视频| 狠狠色综合色综合网络| 欧美激情乱人伦| 亚洲视频一区在线观看| 久久一区中文字幕| 一本色道久久综合亚洲精品婷婷 | 午夜欧美电影在线观看| 久久综合中文| 9人人澡人人爽人人精品| 国产精品亚洲综合一区在线观看| 久久久久久伊人| 一区二区三区国产精品| 久久中文字幕一区二区三区| 一区二区日韩伦理片| 国内精品久久久久久| 久久午夜羞羞影院免费观看| 99国产麻豆精品| 欧美大胆a视频| 新67194成人永久网站| 韩国免费一区| 欧美午夜不卡视频| 久热这里只精品99re8久| 亚洲一区二区欧美| 亚洲高清三级视频| 欧美综合二区| 亚洲一区二区三区精品视频| 亚洲国产精品成人| 欧美日韩你懂的| 午夜精品福利视频| 一本色道精品久久一区二区三区| 亚洲精品视频二区| 在线观看欧美视频| 国产午夜精品一区二区三区视频 | 国产精品成人一区| 欧美成人乱码一区二区三区| 久久国产88| 久久久久成人网| 午夜精品久久久久久久久久久久久 | 在线观看福利一区| 国产嫩草一区二区三区在线观看| 欧美全黄视频| 欧美电影在线观看完整版| 久久精品女人| 亚洲欧美一级二级三级| 妖精视频成人观看www| 亚洲精品免费一二三区| 欧美激情影院| 亚洲国产成人91精品| 欧美激情成人在线| 欧美国产视频日韩| 欧美成人午夜视频| 欧美高清在线一区| 欧美成人午夜| 亚洲第一成人在线| 欧美国产日韩精品| 久久久精品视频成人| 99re热这里只有精品免费视频| 亚洲黄色免费网站| 亚洲精选在线观看| 夜夜躁日日躁狠狠久久88av| 亚洲九九精品| 一区二区不卡在线视频 午夜欧美不卡在 | 欧美性感一类影片在线播放| 欧美成人精品一区| 久久夜色撩人精品| 久久精品视频在线观看| 亚洲另类自拍| 欧美午夜剧场| 国产精品一二三四区| 国产精品一区二区久激情瑜伽| 欧美巨乳波霸| 国产精品视频午夜| 国产免费成人av| 久久久亚洲综合| 久久综合久久久| 亚洲视频www| 欧美区国产区| 国内偷自视频区视频综合| 亚洲日本va午夜在线影院| 亚洲人成小说网站色在线| 亚洲人成在线观看网站高清| 欧美国产激情二区三区| 久久本道综合色狠狠五月| 欧美午夜精品理论片a级按摩| 欧美激情视频在线播放 | 香蕉久久夜色精品国产使用方法| 欧美日韩在线三区| 香蕉av777xxx色综合一区| 性做久久久久久| 欧美在线|欧美| 欧美在线观看网址综合| 亚洲精品中文字| 欧美日韩精品欧美日韩精品| 免费一级欧美在线大片| 久久九九热免费视频| 国产欧美日韩亚洲一区二区三区| 欧美日韩色婷婷|