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

Fork me on GitHub
隨筆 - 215  文章 - 13  trackbacks - 0
<2017年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910


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

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

相冊

Awesome

Blog

Book

GitHub

Link

搜索

  •  

積分與排名

  • 積分 - 219655
  • 排名 - 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 思月行云 閱讀(268) 評論(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>
            久久久www成人免费精品| 亚洲午夜精品网| 卡通动漫国产精品| 在线精品福利| 欧美激情亚洲视频| 欧美国产一区二区三区激情无套| 亚洲精品1区2区| 亚洲精品一级| 国产女主播一区| 欧美成年人在线观看| 欧美久久久久久久| 欧美伊久线香蕉线新在线| 久久国产成人| 99精品国产在热久久下载| 亚洲一区二区精品| 1000部精品久久久久久久久| 亚洲破处大片| 国产精品视频| 欧美激情a∨在线视频播放| 欧美日本在线播放| 久久精品人人| 欧美人与禽猛交乱配| 久久久国产精品一区二区中文| 女人色偷偷aa久久天堂| 亚洲欧美在线观看| 免费不卡视频| 久久国产99| 欧美日韩亚洲天堂| 牛牛国产精品| 国产欧美亚洲一区| 亚洲激情在线视频| 精品成人在线视频| 亚洲丝袜av一区| 欧美在线视频免费| 在线综合+亚洲+欧美中文字幕| 欧美一区二区日韩| 亚洲香蕉网站| 欧美国产亚洲另类动漫| 久久综合精品国产一区二区三区| 欧美日韩综合网| 亚洲国产欧美精品| 在线精品视频一区二区| 午夜欧美大片免费观看| 亚洲视频在线观看视频| 美日韩精品视频免费看| 久久久久久亚洲精品杨幂换脸| 欧美午夜不卡视频| 亚洲精品永久免费精品| 亚洲人成小说网站色在线| 久久久久久久久岛国免费| 午夜宅男欧美| 国产精品看片资源| 亚洲免费不卡| 99热在线精品观看| 欧美大片91| 亚洲激情欧美| 亚洲每日更新| 欧美精品激情| 亚洲人体一区| 一区二区三区回区在观看免费视频| 久久久www| 欧美不卡高清| 亚洲激情在线观看视频免费| 久久亚洲精品伦理| 欧美电影美腿模特1979在线看| 在线不卡视频| 免费看av成人| 亚洲精品久久久久久久久久久 | 亚洲一区二区三区高清| 亚洲影视九九影院在线观看| 欧美日韩精品系列| 日韩一区二区精品葵司在线| 亚洲视频一区在线观看| 欧美喷水视频| 亚洲欧美一区二区三区久久| 久久激情视频久久| 在线国产欧美| 欧美精品一区在线发布| 亚洲深夜福利视频| 久久久人人人| 亚洲国产一区二区a毛片| 欧美伦理一区二区| 亚洲制服av| 麻豆91精品91久久久的内涵| 亚洲精品国产品国语在线app| 欧美日韩国产精品自在自线| 亚洲特黄一级片| 久久这里只有精品视频首页| 亚洲精品美女| 国产伦精品一区二区三区在线观看 | 久久免费高清| 亚洲精品一二| 国产精品影片在线观看| 久久精品五月| 一本色道久久| 久久综合色一综合色88| 9久草视频在线视频精品| 国产精品永久免费观看| 久久精品国产99国产精品澳门| 亚洲欧洲日本mm| 午夜精品久久久久久久99热浪潮 | 欧美午夜宅男影院在线观看| 欧美中文在线观看| 亚洲人成免费| 久久在线播放| 亚洲一区二区网站| 在线看片欧美| 国产午夜精品视频免费不卡69堂| 免费视频一区| 久久久99免费视频| 亚洲免费婷婷| 99热在线精品观看| 欧美a级理论片| 久久成人在线| 99精品视频免费观看视频| 国产日韩欧美不卡在线| 欧美精品在线免费播放| 久久久久久综合| 亚洲欧美日韩视频二区| 99这里只有久久精品视频| 欧美成人视屏| 久久婷婷一区| 久久噜噜噜精品国产亚洲综合 | 亚洲国产欧美一区二区三区丁香婷| 国产精品视频九色porn| 欧美日韩一区二区三区免费看| 美女999久久久精品视频| 久久国产精品第一页| 翔田千里一区二区| 亚洲一二三区精品| 在线一区二区三区四区| 日韩一级在线| 日韩一级黄色av| a4yy欧美一区二区三区| 亚洲免费观看高清完整版在线观看| 欧美韩日一区二区三区| 美国三级日本三级久久99| 久久综合一区二区| 两个人的视频www国产精品| 麻豆精品一区二区av白丝在线| 久久精品导航| 久久综合久久久久88| 老司机成人在线视频| 免费成人av资源网| 欧美国产视频日韩| 亚洲国产专区| 日韩亚洲国产欧美| 99精品欧美一区二区蜜桃免费| av成人免费| 亚洲欧美日韩综合国产aⅴ| 性做久久久久久久久| 久久精品72免费观看| 米奇777在线欧美播放| 欧美激情久久久久久| 欧美日韩国产一区精品一区 | 国产日本欧美一区二区三区在线| 国产麻豆日韩| 永久免费毛片在线播放不卡| 亚洲片在线观看| 亚洲网站啪啪| 久久精品国产综合| 欧美国产精品劲爆| 99在线精品免费视频九九视| 一区二区三区日韩| 欧美在线免费观看亚洲| 毛片一区二区| 国产精品毛片a∨一区二区三区| 国产亚洲成av人片在线观看桃| 激情欧美一区二区| 99国产精品久久久久久久| 午夜精品福利在线| 久久天堂成人| 日韩一级片网址| 欧美在线视频一区二区| 欧美另类极品videosbest最新版本| 国产精品欧美激情| 亚洲国产一区二区a毛片| 亚洲自拍都市欧美小说| 免费久久99精品国产自| 中文一区二区在线观看| 久久精品女人天堂| 欧美视频一区二区| 在线观看中文字幕不卡| 午夜免费日韩视频| 亚洲国产精品99久久久久久久久| 亚洲视频1区| 欧美第一黄色网| 国产真实久久| 亚洲专区一区| 亚洲精品综合精品自拍| 久久天天躁狠狠躁夜夜爽蜜月| 国产精品萝li| 亚洲精一区二区三区| 久久综合伊人77777尤物| 亚洲自拍偷拍一区| 欧美伦理91i| 亚洲日本成人网| 欧美成年人网站| 久久精品国内一区二区三区| 国产精品热久久久久夜色精品三区 |