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

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>
            久久久久久高潮国产精品视| 欧美激情二区三区| 亚洲裸体俱乐部裸体舞表演av| 久久国产精品久久w女人spa| 国产主播喷水一区二区| 欧美中文日韩| 香蕉久久一区二区不卡无毒影院| 国产精品亚洲视频| 欧美中文字幕第一页| 午夜宅男欧美| 国产一区二区精品在线观看| 久久久久国产精品午夜一区| 久久久久9999亚洲精品| 亚洲国产清纯| 99riav久久精品riav| 国产精品一卡二| 久久五月激情| 欧美黄色网络| 欧美一区国产二区| 久久久久久久久久久久久9999| 亚洲国产成人不卡| 99精品免费| 国产日本欧美一区二区三区| 蜜桃久久精品乱码一区二区| 欧美国产精品久久| 欧美专区第一页| 蜜臀久久久99精品久久久久久 | 久久久久一区二区三区| 亚洲第一页自拍| 亚洲美女在线国产| 国产日韩精品久久久| 亚洲国产婷婷综合在线精品| 欧美不卡高清| 香蕉av福利精品导航| 久久资源在线| 亚洲在线观看免费| 久久久亚洲国产天美传媒修理工 | 亚洲国语精品自产拍在线观看| 亚洲日韩成人| 狠狠狠色丁香婷婷综合久久五月| 亚洲激情六月丁香| 国内偷自视频区视频综合| 亚洲国产日韩欧美| 国产一区二区丝袜高跟鞋图片| 亚洲国产另类久久久精品极度| 国产麻豆视频精品| 亚洲国产成人精品久久久国产成人一区| 国产精品成人aaaaa网站| 久久综合色婷婷| 国产精品久久久亚洲一区 | 亚洲亚洲精品在线观看| 免费在线亚洲欧美| 久久精品水蜜桃av综合天堂| 欧美日韩在线综合| 欧美激情乱人伦| 国产综合欧美| 亚洲综合电影| 亚洲网站视频福利| 欧美国产在线电影| 男男成人高潮片免费网站| 国产精品一区二区三区久久久| 91久久精品网| 91久久在线| 久久综合激情| 久久亚洲美女| 激情欧美亚洲| 久久精品亚洲一区二区三区浴池| 欧美一二三区在线观看| 欧美亚韩一区| 亚洲最新中文字幕| 中文网丁香综合网| 欧美精品xxxxbbbb| 亚洲青涩在线| 99视频精品全国免费| 欧美精品在线免费| 亚洲精品一二区| 一本色道久久| 欧美日韩免费观看一区三区 | 久久久综合免费视频| 国产一区二区三区网站| 欧美在线免费一级片| 久久久久久久综合| 在线观看视频欧美| 久色成人在线| 91久久久亚洲精品| 在线亚洲观看| 国产精品丝袜xxxxxxx| 亚洲男人的天堂在线观看| 欧美有码视频| 伊人天天综合| 欧美激情亚洲另类| 亚洲免费成人| 欧美在线一二三区| 一区二区视频免费在线观看| 欧美成人免费大片| aa国产精品| 久久精品国产综合| 亚洲激情在线激情| 欧美天堂在线观看| 久久精品99久久香蕉国产色戒| 美女脱光内衣内裤视频久久网站| 亚洲国产另类久久久精品极度| 欧美日韩国产成人在线免费| 亚洲网友自拍| 欧美电影免费观看高清| 日韩午夜电影| 国产一区二区黄色| 欧美国产视频在线| 性做久久久久久久免费看| 欧美大片免费久久精品三p| 在线亚洲精品福利网址导航| 国产亚洲人成a一在线v站| 欧美成人dvd在线视频| 亚洲天堂偷拍| 欧美激情精品久久久久久免费印度 | 宅男在线国产精品| 欧美aaa级| 亚洲午夜精品久久久久久app| 国内精品视频在线观看| 欧美日韩色一区| 久久精品国产一区二区三区| 夜夜嗨av一区二区三区四季av| 久久男女视频| 亚洲欧美欧美一区二区三区| 在线看一区二区| 国产精品视频自拍| 欧美激情性爽国产精品17p| 欧美一级网站| 亚洲午夜一二三区视频| 亚洲高清视频中文字幕| 久久久91精品国产| 亚洲一区二区精品在线观看| 亚洲欧洲一区二区在线播放| 国产日韩精品入口| 国产精品成人v| 欧美日韩国产综合视频在线观看 | 99国产精品久久久久久久成人热| 免费观看日韩| 久久久夜色精品亚洲| 欧美专区18| 午夜精品一区二区三区四区 | 国产日韩欧美不卡在线| 欧美日韩一区高清| 欧美日本一区| 欧美区在线播放| 欧美激情1区2区3区| 免费在线看一区| 免费精品视频| 久久亚洲二区| 久久久亚洲影院你懂的| 欧美亚洲一区在线| 羞羞色国产精品| 亚洲欧美日韩精品久久| 亚洲自拍电影| 欧美一区二区| 久久精品人人爽| 久久成人国产精品| 久久影院午夜片一区| 久久久久久久999| 老**午夜毛片一区二区三区| 久久久久一区二区三区| 久久免费国产精品| 欧美成人在线免费视频| 欧美日本在线播放| 欧美日韩在线一区二区三区| 国产精品区一区二区三| 国产老女人精品毛片久久| 国产午夜亚洲精品羞羞网站| 精品动漫3d一区二区三区| 一区二区自拍| 亚洲精品一区二区三区在线观看| av成人黄色| 午夜日韩福利| 免费成人在线观看视频| 亚洲国产导航| 在线综合亚洲| 久久精品官网| 欧美激情精品久久久六区热门 | 亚洲在线第一页| 久久精品道一区二区三区| 欧美不卡在线| 国产精品乱码一区二三区小蝌蚪 | 久久综合九色99| 欧美劲爆第一页| 国产精品综合不卡av| 狠狠色伊人亚洲综合成人| 日韩一区二区精品在线观看| 亚洲一区二区三区中文字幕在线| 久久激情五月激情| 久久日韩粉嫩一区二区三区| 欧美日韩三级| 国产原创一区二区| 99精品国产热久久91蜜凸| 久久xxxx精品视频| 亚洲人成网站精品片在线观看| 亚洲一区二区三区777| 老司机一区二区三区| 国产精品视频大全| 在线免费观看日韩欧美| 亚洲欧美国产毛片在线|