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

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


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

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

相冊

Awesome

Blog

Book

GitHub

Link

搜索

  •  

積分與排名

  • 積分 - 221466
  • 排名 - 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 思月行云 閱讀(277) 評論(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>
            久久理论片午夜琪琪电影网| 国产欧美日韩精品专区| 一区二区三区自拍| 久久亚洲私人国产精品va| 欧美一区二区观看视频| 国语自产在线不卡| 免费日本视频一区| 免费不卡亚洲欧美| 亚洲手机视频| 午夜久久美女| 亚洲国产另类 国产精品国产免费| 欧美成人69av| 欧美四级电影网站| 久久九九国产精品| 欧美激情第10页| 欧美一区二区在线看| 久久视频在线免费观看| 在线亚洲精品| 久久www免费人成看片高清 | 久久九九国产精品怡红院| 亚洲国产mv| 一区二区不卡在线视频 午夜欧美不卡'| 国产精品草莓在线免费观看| 久久免费视频观看| 欧美日韩另类丝袜其他| 久久蜜桃资源一区二区老牛 | 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久精品91| 日韩一二三区视频| 欧美在线免费看| 亚洲午夜激情| 免费在线视频一区| 久久大逼视频| 欧美日韩精品一本二本三本| 久久综合色影院| 国产精品久久久久一区| 亚洲国产成人精品久久| 国产精品视频一| 亚洲精品美女在线观看| 在线日韩中文| 欧美一区视频| 午夜精品影院| 欧美偷拍一区二区| 亚洲人成网站999久久久综合| 韩国成人精品a∨在线观看| 国产精品99久久久久久人| 亚洲欧洲一区二区三区在线观看 | 一区二区三区欧美成人| 亚洲国产欧美一区二区三区久久| 午夜精品av| 亚洲欧美大片| 欧美日韩亚洲一区二区三区在线观看| 欧美成人tv| 一区二区在线免费观看| 欧美在线一级va免费观看| 亚洲在线免费观看| 欧美视频免费在线| 日韩视频永久免费观看| 一本一本久久a久久精品综合妖精| 美女脱光内衣内裤视频久久网站| 久久亚洲风情| 尤物九九久久国产精品的特点| 亚洲欧美日韩一区二区在线| 亚洲男人的天堂在线aⅴ视频| 欧美色大人视频| 99精品视频免费全部在线| 在线亚洲一区二区| 国产精品xxxav免费视频| 一区二区三区蜜桃网| 亚洲欧美日韩一区| 国产精品毛片| 欧美有码视频| 麻豆精品一区二区av白丝在线| 伊人激情综合| 模特精品裸拍一区| 日韩网站在线| 欧美怡红院视频| 狠色狠色综合久久| 免费美女久久99| 99精品视频免费| 欧美在线视频不卡| 国一区二区在线观看| 免费观看成人www动漫视频| 亚洲欧洲一区二区在线观看 | 有码中文亚洲精品| 免费成人性网站| 夜夜嗨av一区二区三区中文字幕 | 亚洲国产精品va| 亚洲无限乱码一二三四麻| 国产精品一区二区你懂得| 欧美在线观看一二区| 欧美激情女人20p| 亚洲欧美久久久| 激情自拍一区| 欧美日韩在线播放三区| 午夜亚洲激情| 亚洲精品永久免费| 久久国产日韩欧美| 亚洲精选久久| 国产午夜精品福利| 欧美精品免费看| 欧美在线免费观看亚洲| 亚洲靠逼com| 玖玖玖国产精品| 亚洲欧美在线免费观看| 亚洲激情影视| 国内精品一区二区| 欧美调教视频| 欧美大片在线看| 欧美一区二区三区四区在线观看| 亚洲国产高清一区二区三区| 欧美一级视频免费在线观看| 亚洲人成网站777色婷婷| 国产精品自拍在线| 欧美日韩成人综合| 狂野欧美一区| 欧美中文字幕精品| 亚洲一区国产| 99re热精品| 亚洲日本欧美在线| 欧美第一黄网免费网站| 欧美在线观看视频在线| 亚洲一区二区三区视频| 亚洲精品免费电影| 亚洲国产va精品久久久不卡综合| 国产精品自拍在线| 欧美少妇一区| 欧美揉bbbbb揉bbbbb| 欧美国产精品| 欧美电影专区| 欧美成人午夜| 欧美大片在线观看| 欧美xxx成人| 美女网站久久| 美日韩精品免费观看视频| 久久九九精品99国产精品| 欧美一区成人| 欧美一级成年大片在线观看| 亚洲欧美日韩专区| 亚洲综合日韩中文字幕v在线| 一区二区欧美日韩| 亚洲天堂av在线免费| 制服丝袜亚洲播放| 亚洲少妇中出一区| 亚洲一区二区久久| 亚洲欧美日韩在线综合| 香蕉久久夜色精品| 久久久成人网| 欧美成人tv| 欧美精品一区二区三区在线看午夜 | 亚洲午夜免费福利视频| 亚洲一卡二卡三卡四卡五卡| 亚洲免费在线| 欧美在线观看视频在线| 久久久久久久久久久久久9999| 久久av二区| 免费观看日韩| 欧美日韩一区二区在线播放| 国产精品毛片a∨一区二区三区| 国产精品永久免费在线| 国产资源精品在线观看| 亚洲电影下载| 国产精品99久久久久久久女警 | 亚洲专区一二三| 欧美专区日韩专区| 男女av一区三区二区色多| 亚洲国产日韩欧美| 亚洲一区二区三区精品在线观看| 香蕉久久一区二区不卡无毒影院 | 国产精品草草| 好看的亚洲午夜视频在线| 亚洲激情视频在线| 亚洲在线一区二区三区| 久久尤物视频| 日韩视频永久免费观看| 欧美一区二区精美| 欧美日本二区| 韩国欧美国产1区| 亚洲图片欧美日产| 久久深夜福利| 99精品福利视频| 久久男女视频| 国产精品嫩草久久久久| 亚洲丰满在线| 性高湖久久久久久久久| 亚洲第一精品影视| 亚洲欧美日本精品| 欧美区日韩区| 亚洲二区免费| 久久av一区二区三区亚洲| 最新国产成人在线观看 | 欧美成人一区二区| 亚洲视频免费看| 欧美岛国激情| 韩国av一区二区| 欧美亚洲一级片| 日韩手机在线导航| 欧美成人一区二区| 在线观看成人av电影| 欧美在线视频播放|