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

Fork me on GitHub
隨筆 - 215  文章 - 13  trackbacks - 0
<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


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

常用鏈接

留言簿(1)

隨筆分類

隨筆檔案

相冊

Awesome

Blog

Book

GitHub

Link

搜索

  •  

積分與排名

  • 積分 - 220982
  • 排名 - 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 思月行云 閱讀(273) 評論(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伊人久久综合| 亚洲韩国日本中文字幕| 亚洲综合社区| 亚洲一区二区高清| 国产欧美日韩亚洲一区二区三区| 亚洲尤物在线| 亚洲一区国产一区| 国产区日韩欧美| 久色成人在线| 欧美激情精品久久久六区热门| 亚洲另类自拍| 亚洲一区成人| 一区在线视频观看| 亚洲激情午夜| 国产精品一区二区黑丝| 久热精品视频在线观看一区| 欧美成人免费在线视频| 亚洲愉拍自拍另类高清精品| 午夜精品久久久久久久久久久久| 精品91在线| 亚洲欧洲精品一区| 国产午夜精品美女视频明星a级| 女人香蕉久久**毛片精品| 欧美日本高清| 久久久国产精品一区| 欧美高清你懂得| 欧美专区在线播放| 欧美精品电影| 久久精品首页| 欧美三区在线观看| 老司机午夜精品| 国产精品伦一区| 亚洲国产综合视频在线观看| 国产精品久久久久久福利一牛影视| 麻豆av一区二区三区| 欧美精品播放| 你懂的国产精品永久在线| 国产精品久久久久99| 亚洲缚视频在线观看| 国产婷婷色综合av蜜臀av| 亚洲精品美女免费| 伊人成人开心激情综合网| 国产精品99久久久久久久久久久久| 一区二区视频免费在线观看| 中国成人黄色视屏| 99精品免费网| 欧美gay视频激情| 美女图片一区二区| 国产一区二区欧美日韩| 亚洲一区二区精品在线观看| avtt综合网| 欧美高清视频www夜色资源网| 久久免费高清| 国产午夜精品一区二区三区视频| 99精品欧美一区二区三区综合在线| 一色屋精品视频在线看| 欧美在线观看视频一区二区| 亚洲视频网在线直播| 欧美激情在线狂野欧美精品| 欧美成人免费在线| 精品福利电影| 久久久99免费视频| 久久午夜国产精品| 国产一区二区毛片| 久久电影一区| 久久久久久伊人| 好吊色欧美一区二区三区四区| 亚洲欧美综合一区| 久久精品理论片| 国产综合色精品一区二区三区| 亚洲欧美日韩高清| 久久精品国产第一区二区三区最新章节| 欧美性开放视频| 亚洲免费网站| 久久久www成人免费精品| 国产色视频一区| 久久久久久综合| 亚洲成人中文| 一区二区三区久久| 国产精品视频精品| 先锋影音国产一区| 鲁大师成人一区二区三区 | 亚洲国产中文字幕在线观看| 日韩视频―中文字幕| 欧美精品一区二区三| 日韩视频免费观看高清完整版| 亚洲性感美女99在线| 国产精品婷婷午夜在线观看| 久久动漫亚洲| 亚洲激情在线视频| 香蕉成人伊视频在线观看| 国产日韩精品久久| 老司机一区二区| 99视频在线观看一区三区| 欧美中文字幕第一页| 亚洲成人中文| 国产精品成人在线观看| 欧美中文字幕久久| 亚洲国产欧美不卡在线观看| 亚洲一区中文字幕在线观看| 国产私拍一区| 欧美伦理a级免费电影| 亚欧成人在线| 亚洲精品国产品国语在线app | 国产精品成人aaaaa网站| 亚洲综合日本| 亚洲欧洲精品一区| 久久久人成影片一区二区三区| 亚洲精品日本| 国产日韩精品在线观看| 欧美精品电影| 久久青草久久| 亚洲欧美日韩在线不卡| 亚洲精品一区二区三区蜜桃久| 久久爱www| 亚洲欧美另类国产| 亚洲精品一区在线观看香蕉| 国产亚洲综合在线| 欧美午夜精品久久久久久孕妇| 久久久青草青青国产亚洲免观| av成人免费| 亚洲日本电影| 欧美国产日韩a欧美在线观看| 午夜视频在线观看一区二区| 99综合精品| 亚洲国内自拍| 黑人一区二区| 欧美午夜三级| 久久伊人亚洲| 欧美成人精品激情在线观看| 久久精品最新地址| 夜夜夜久久久| 亚洲高清毛片| 国产一区二区精品| 国产日韩视频| 欧美色网在线| 欧美风情在线观看| 99re热精品| 一本色道综合亚洲| 欧美大色视频| 久久久蜜臀国产一区二区| 9久re热视频在线精品| 日韩亚洲欧美成人| 精品动漫av| 国产在线精品一区二区夜色| 欧美日韩精品一区二区| 欧美日韩不卡| 鲁大师成人一区二区三区| 午夜激情一区| 国产精品99久久99久久久二8 | 亚洲伦伦在线| 国产欧美日韩视频一区二区三区| 欧美激情一区二区三区在线视频| 午夜欧美大尺度福利影院在线看| 亚洲人成在线播放| 亚洲精品欧美极品| 亚洲国产天堂久久国产91| 久久影视精品| 久久噜噜噜精品国产亚洲综合| 一区二区三区视频在线看| 艳女tv在线观看国产一区| 亚洲国产综合在线看不卡| 伊人久久综合97精品| 欧美日韩亚洲一区二区| 欧美日韩人人澡狠狠躁视频| 欧美精品一区在线观看| 欧美精品成人一区二区在线观看| 可以看av的网站久久看| 久久激情综合网| 久久精品1区| 久久久噜噜噜久久中文字幕色伊伊| 欧美一级片在线播放| 久久精品青青大伊人av| 久久久久久久久一区二区| 久久精品水蜜桃av综合天堂| 亚洲图片欧美午夜| 久久久爽爽爽美女图片| 老司机一区二区| 欧美1级日本1级| 欧美一区国产一区| 欧美精品偷拍| 欧美午夜视频网站| 国产精品一二| 国产亚洲美州欧州综合国| 国产精品午夜在线| 一区二区在线视频观看| 亚洲欧洲一区二区天堂久久 | 欧美亚洲一区二区在线观看| 亚洲国产1区| 亚洲欧美日韩专区| 久久躁狠狠躁夜夜爽| 免费日本视频一区| 欧美午夜一区二区| 亚洲日本aⅴ片在线观看香蕉| 日韩亚洲国产欧美| 久久av一区二区三区漫画| 香蕉乱码成人久久天堂爱免费 | 欧美韩国日本一区| 国产精品久久久久久久久久免费看 | 国产精品亚洲аv天堂网|