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

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

搜索

  •  

積分與排名

  • 積分 - 219653
  • 排名 - 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>
            欧美一区二区女人| 99国产精品99久久久久久粉嫩| 一区二区三区 在线观看视| 久久亚洲高清| 久久精品导航| 激情视频一区二区| 久久久欧美一区二区| 久久se精品一区精品二区| 国产三区精品| 久久久久久久999| 亚洲性xxxx| 欧美久久久久久久久| 9久re热视频在线精品| 亚洲精选中文字幕| 欧美日韩另类综合| 中文在线一区| 亚洲欧美日韩国产成人| 狠狠色丁香婷婷综合久久片| 香蕉视频成人在线观看| 欧美一区二区精品在线| 黄色精品在线看| 亚洲国产精品va在看黑人| 欧美激情久久久| 亚洲午夜高清视频| 欧美一区二区精品久久911| 国产欧美精品| 乱码第一页成人| 欧美高清视频在线播放| 亚洲综合第一页| 欧美在线不卡视频| 91久久国产自产拍夜夜嗨| 亚洲欧洲一区二区在线播放| 国产精品国色综合久久| 毛片一区二区三区| 欧美日韩色综合| 久久久久久久久岛国免费| 欧美插天视频在线播放| 亚洲免费在线播放| 性伦欧美刺激片在线观看| 亚洲精品久久在线| 亚洲一区在线免费| 亚洲第一色在线| 一本久久a久久免费精品不卡| 国产日韩欧美高清免费| 欧美韩日一区| 国产精品视频免费观看www| 欧美国产日韩a欧美在线观看| 欧美日韩情趣电影| 免费中文日韩| 国产精品久久久久久久久久免费看 | 久久国产精品99国产精| 免费看亚洲片| 久久精品盗摄| 欧美日韩国产三区| 老司机精品福利视频| 欧美日韩亚洲一区三区| 久久视频在线视频| 国产精品女人网站| 欧美国产一区视频在线观看| 国产欧美日韩专区发布| 日韩视频在线观看一区二区| 亚洲高清av在线| 久久精品成人欧美大片古装| 亚洲精选一区二区| 裸体丰满少妇做受久久99精品| 午夜精品在线| 欧美调教vk| 日韩小视频在线观看| 最新精品在线| 久久综合导航| 久久久久久综合| 国产亚洲精品aa| 亚洲欧美大片| 一区二区三区不卡视频在线观看| 久久综合久久久久88| 久久亚洲视频| 好吊妞这里只有精品| 欧美一区二区黄| 亚洲图片欧美日产| 欧美午夜精品久久久久免费视| 亚洲国产精品va| 正在播放欧美一区| 欧美日韩一区二区三区四区在线观看| 亚洲电影视频在线| 亚洲看片免费| 欧美另类一区二区三区| 亚洲精品乱码久久久久久日本蜜臀| 亚洲精品国久久99热| 欧美电影打屁股sp| 亚洲精品偷拍| 亚洲天堂网站在线观看视频| 国产精品久久久久久久一区探花| 一区二区精品国产| 欧美在线播放一区| 影音先锋亚洲一区| 欧美大色视频| 亚洲四色影视在线观看| 欧美伊久线香蕉线新在线| 国产啪精品视频| 久久亚洲不卡| 亚洲精品日本| 欧美在线播放一区| 136国产福利精品导航网址| 欧美777四色影视在线| aa级大片欧美三级| 欧美专区亚洲专区| 国产综合av| 免费在线国产精品| 亚洲图片欧美一区| 欧美激情麻豆| 老司机精品视频网站| 午夜免费日韩视频| 一区二区国产精品| 亚洲激情视频在线| 狠狠色狠狠色综合日日91app| 欧美日韩在线视频首页| 男女精品网站| 久久久之久亚州精品露出| 亚洲欧美日韩在线不卡| av成人免费在线| 亚洲国产欧美日韩精品| 你懂的成人av| 久久久久久久激情视频| 欧美影院在线播放| 午夜精品久久久久久久白皮肤| 亚洲毛片av| 亚洲欧洲美洲综合色网| 亚洲第一综合天堂另类专| 国产在线乱码一区二区三区| 国产精品日韩欧美大师| 欧美三级视频| 欧美天天视频| 国产精品久久久久9999吃药| 欧美日韩国产精品自在自线| 欧美精品乱人伦久久久久久| 免费在线亚洲| 欧美激情亚洲另类| 欧美激情国产日韩| 欧美激情一区在线观看| 欧美激情免费在线| 欧美日韩高清区| 欧美日韩国产精品一区二区亚洲| 欧美日韩国产区一| 国产精品国产a| 国产精品欧美一区喷水| 国产欧美日本一区二区三区| 国产日本欧美一区二区三区| 国产主播在线一区| 在线日韩av| 洋洋av久久久久久久一区| 亚洲综合999| 久久精品国产77777蜜臀| 久久视频一区二区| 欧美激情一区二区三级高清视频| 亚洲激情一区| 亚洲天堂av在线免费| 欧美在线影院| 欧美大片网址| 国产精品久久久久久久免费软件 | 亚洲欧美欧美一区二区三区| 欧美一激情一区二区三区| 久久人体大胆视频| 欧美日韩亚洲天堂| 国产欧美视频一区二区三区| 一区免费观看视频| 99在线精品观看| 香蕉精品999视频一区二区| 久久综合狠狠综合久久综青草| 欧美黄色日本| 亚洲一区二区三区激情| 久久免费视频在线| 欧美日韩一区二区在线观看| 国产日韩欧美一区二区三区在线观看| 激情久久影院| 亚洲视屏在线播放| 免费成人高清| 亚洲午夜精品网| 巨乳诱惑日韩免费av| 国产精品伦一区| 亚洲欧洲视频| 久久精彩视频| 亚洲美女在线视频| 久久久欧美精品| 国产精品露脸自拍| 亚洲精品午夜精品| 久久躁日日躁aaaaxxxx| 一区二区三区高清不卡| 免费成人av资源网| 国产区日韩欧美| 亚洲一区在线直播| 欧美黄色大片网站| 欧美中文字幕不卡| 国产精品一卡| 亚洲一区二区三区久久| 亚洲二区在线| 久久久久免费视频| 国产亚洲人成a一在线v站| 亚洲一区二区在线看| 亚洲人成小说网站色在线| 久久一区二区三区四区|