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

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>
            欧美日韩在线影院| 国产精品久久久久久av下载红粉 | 欧美日精品一区视频| 99re66热这里只有精品4| 亚洲国产你懂的| 女人天堂亚洲aⅴ在线观看| 91久久在线视频| 日韩一级欧洲| 国产精品一区二区a| 久久精品免费电影| 老司机精品久久| 99在线精品视频| 亚洲一区激情| 激情国产一区| 亚洲国产欧美在线| 国产精品扒开腿做爽爽爽视频| 午夜精品一区二区三区四区 | 亚洲激情自拍| 欧美视频中文字幕| 久久精品女人天堂| 欧美成人亚洲成人日韩成人| 亚洲午夜羞羞片| 久久久不卡网国产精品一区| 亚洲精品黄色| 亚洲淫片在线视频| 在线免费观看视频一区| 亚洲毛片av在线| 国产一区二区激情| 日韩一级成人av| 经典三级久久| 一区二区三区成人精品| 国产在线麻豆精品观看| 亚洲三级免费电影| 精品电影一区| 亚洲永久免费| 日韩视频一区二区三区| 久久www成人_看片免费不卡| 一区二区欧美精品| 久久婷婷综合激情| 欧美在线观看网址综合| 欧美国产日韩一区二区在线观看| 欧美在线|欧美| 欧美乱人伦中文字幕在线| 久久夜色精品国产| 国产欧美 在线欧美| 最新精品在线| 在线精品福利| 亚洲免费观看视频| 午夜伦欧美伦电影理论片| 免费欧美电影| 国产精品欧美久久| 亚洲国产国产亚洲一二三| 国产目拍亚洲精品99久久精品| 亚洲日本成人| 亚洲福利视频二区| 久久精品99国产精品| 午夜精品一区二区在线观看| 欧美日韩免费观看一区三区| 亚洲高清在线视频| 亚洲激情在线观看| 牛人盗摄一区二区三区视频| 老**午夜毛片一区二区三区| 国产一区二区黄| 羞羞漫画18久久大片| 午夜亚洲精品| 国产欧美在线观看一区| 亚洲欧美视频| 久久精品国产96久久久香蕉| 国产无一区二区| 欧美在线视频免费播放| 久久久久久久91| 激情视频一区| 毛片av中文字幕一区二区| 欧美激情第10页| 亚洲理论电影网| 欧美日韩一级黄| 亚洲视频一区二区| 欧美一区二区三区日韩| 国产亚洲激情在线| 久久久精品动漫| 欧美国产精品| 亚洲五月婷婷| 国产亚洲va综合人人澡精品| 欧美专区在线| 亚洲福利视频三区| 99精品欧美| 国产伦精品一区二区三区免费迷 | 亚洲视频999| 午夜视频久久久久久| 国产自产在线视频一区| 久久在线观看视频| 亚洲精品四区| 欧美在线一二三区| 亚洲黑丝在线| 国产精品亚洲成人| 久久一区二区视频| 一区二区三区欧美视频| 久久久噜噜噜久久久| 亚洲欧洲精品一区二区三区不卡 | 一区二区视频在线观看| 欧美aaaaaaaa牛牛影院| 一本色道久久综合狠狠躁篇怎么玩 | 国产色综合久久| 久久资源av| 亚洲婷婷在线| 欧美大片在线观看一区| 亚洲欧美日韩一区| 亚洲国产欧美一区| 国产精品入口尤物| 欧美第一黄网免费网站| 午夜精品视频在线| 亚洲日本欧美日韩高观看| 欧美一区网站| 在线亚洲免费视频| 亚洲第一毛片| 国产精品三上| 欧美日韩一级黄| 美乳少妇欧美精品| 久久国产精品亚洲77777| 一本色道88久久加勒比精品| 欧美va亚洲va国产综合| 欧美一区二视频在线免费观看| 日韩天堂av| 亚洲黄网站在线观看| 国产一区二区三区久久 | 久久精品一区蜜桃臀影院 | 国产一区二区三区久久悠悠色av | 欧美日韩一区二区欧美激情| 欧美夜福利tv在线| 亚洲视频在线视频| 亚洲美女毛片| 亚洲精品韩国| 亚洲国产专区| 亚洲国产精品久久久久婷婷884| 另类av导航| 久久久久久一区二区| 亚洲欧美国产高清| 亚洲桃花岛网站| 一区二区三区不卡视频在线观看 | 在线观看欧美视频| 精品91久久久久| 黄色亚洲精品| 伊人成人在线视频| 精品二区视频| 在线欧美三区| 亚洲国产婷婷香蕉久久久久久| 黄色成人av| 亚洲第一色在线| 亚洲黄色精品| 夜夜嗨av一区二区三区| 一区二区三区视频免费在线观看| 99视频有精品| 亚洲专区免费| 久久精品国产99精品国产亚洲性色| 羞羞色国产精品| 久久久国产午夜精品| 久久婷婷成人综合色| 麻豆久久久9性大片| 欧美成人国产| 亚洲精品偷拍| 亚洲欧美国产日韩天堂区| 午夜精品理论片| 久久久久久久久久久久久女国产乱 | 欧美一级视频一区二区| 久久国产视频网站| 欧美电影免费观看大全| 欧美性做爰毛片| 国产一区二区三区直播精品电影 | 亚洲男人影院| 久久久久国产一区二区| 美女脱光内衣内裤视频久久影院 | 一本不卡影院| 久久9热精品视频| 欧美成人69| 国产精品专区h在线观看| 国外成人在线视频| 99热在这里有精品免费| 欧美在线视频全部完| 欧美激情在线观看| 亚洲专区一区| 欧美国产一区视频在线观看| 国产精品一区2区| 亚洲精品中文字幕有码专区| 欧美亚洲视频在线看网址| 欧美高清在线一区二区| 亚洲永久在线| 欧美大尺度在线观看| 国产亚洲欧美中文| 一区二区三区精品视频| 美女久久一区| 亚洲在线日韩| 欧美日韩国产精品自在自线| 狠狠色狠狠色综合人人| 亚洲淫片在线视频| 亚洲韩国青草视频| 欧美一区三区二区在线观看| 欧美日韩一区二区欧美激情| 伊人成人在线视频| 久久国产手机看片| 亚洲视频中文字幕|