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

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>
            午夜精品偷拍| 久久久久久国产精品mv| 欧美日韩成人精品| 一区二区日韩精品| 一区二区欧美亚洲| 欧美日韩综合在线| 午夜一区二区三区在线观看| 亚洲手机成人高清视频| 国产精品专区一| 久久精品123| 久久久久成人精品| 亚洲精品国产视频| 99国产精品国产精品毛片| 欧美性色综合| 久久久综合香蕉尹人综合网| 久久亚洲精选| 日韩亚洲欧美在线观看| 亚洲视频www| 激情亚洲网站| 日韩视频免费在线观看| 国产精品一区二区久久| 免播放器亚洲| 欧美日韩午夜剧场| 欧美在线播放一区二区| 美女精品视频一区| 亚洲专区欧美专区| 久久人人九九| 亚洲一区二区三区视频播放| 久久精品欧美日韩精品| 一本一本a久久| 欧美在线3区| 亚洲一区二区三区欧美 | 最新国产の精品合集bt伙计| 欧美激情中文字幕乱码免费| 国产精品成人国产乱一区| 久久久久久9999| 欧美人与禽性xxxxx杂性| 久久久av毛片精品| 欧美日韩中文在线| 葵司免费一区二区三区四区五区| 欧美日韩精品免费观看| 久久综合久久综合久久| 国产精品久久久久秋霞鲁丝 | 亚洲国产老妈| 小嫩嫩精品导航| 亚洲国产精品传媒在线观看| 亚洲女优在线| 亚洲特级毛片| 久久综合久久久久88| 欧美一级电影久久| 欧美色图五月天| 亚洲国产成人久久综合| 国产亚洲欧洲| 亚洲综合色自拍一区| 一区二区三区久久久| 美女国产一区| 欧美成人三级在线| 韩日精品中文字幕| 西瓜成人精品人成网站| 亚洲自拍偷拍视频| 欧美日韩一二三区| 欧美激情小视频| 亚洲国产高清在线| 久久久久久久久久码影片| 久久久久se| 国产在线视频欧美| 欧美亚洲一区三区| 久久精品一区二区| 国产专区一区| 久久婷婷综合激情| 欧美第一黄网免费网站| 亚洲福利国产| 女生裸体视频一区二区三区| 欧美国产日韩xxxxx| 在线看国产日韩| 免费成人你懂的| 亚洲国产另类久久精品| 99视频超级精品| 欧美日韩免费看| 亚洲视频一二三| 欧美淫片网站| 激情欧美一区| 欧美成人国产一区二区| 亚洲日本va午夜在线影院| 一本综合精品| 国产伦理一区| 久久久中精品2020中文| 欧美激情一区二区三区全黄| 99精品欧美一区二区三区综合在线| 欧美精品一区二区三区四区| 一区二区三区蜜桃网| 久久精品国产99| 亚洲国产精品va在线看黑人 | 一卡二卡3卡四卡高清精品视频| 99这里有精品| 国产欧美一区二区三区视频| 久久久久国产成人精品亚洲午夜| 欧美激情一二三区| 亚洲免费视频观看| 国产伊人精品| 欧美激情综合五月色丁香| 一区二区三区精品久久久| 欧美在线视频在线播放完整版免费观看| 国产亚洲精品7777| 欧美激情bt| 午夜日韩电影| 亚洲精品在线观| 久久免费黄色| 亚洲在线视频一区| 久久黄色小说| 亚洲精品中文字幕女同| 国产麻豆一精品一av一免费| 免费亚洲视频| 欧美一区二区久久久| 亚洲激情成人| 久久精品一区二区三区四区| 99热这里只有精品8| 红桃视频欧美| 国产麻豆日韩| 欧美日韩亚洲一区二区三区在线观看| 久久精品国产99精品国产亚洲性色| 最新亚洲电影| 欧美成人乱码一区二区三区| 先锋亚洲精品| 亚洲一卡久久| 亚洲精品乱码视频| 娇妻被交换粗又大又硬视频欧美| 国产精品福利网站| 免费看黄裸体一级大秀欧美| 久久成人综合视频| 亚洲综合欧美日韩| 一区二区三区四区国产| 91久久精品视频| 欧美国产先锋| 欧美电影免费观看大全| 久久亚洲私人国产精品va| 欧美亚洲色图校园春色| 亚洲一区二区三区午夜| 亚洲另类视频| 日韩视频在线免费| 亚洲毛片在线看| 亚洲国产精品久久久久| 亚洲二区在线观看| 在线成人中文字幕| 伊甸园精品99久久久久久| 国内外成人免费视频| 国产日韩欧美日韩| 国产有码一区二区| 国内精品视频在线观看| 激情久久久久久| 精品成人一区二区| 在线看片日韩| 亚洲日本中文字幕区| 日韩一区二区精品葵司在线| 亚洲美洲欧洲综合国产一区| 亚洲美女视频在线观看| 亚洲免费观看| 亚洲网站在线播放| 亚洲欧美国产日韩天堂区| 羞羞漫画18久久大片| 欧美在线播放高清精品| 久久综合九九| 欧美激情亚洲自拍| 艳女tv在线观看国产一区| 在线视频精品一区| 午夜精品久久久久久久白皮肤| 欧美一区二区三区免费观看| 欧美在线观看视频一区二区| 久久久最新网址| 欧美巨乳在线观看| 国产美女在线精品免费观看| 韩国一区电影| 一本色道久久综合| 久久精品免费| 亚洲电影av在线| 亚洲伊人第一页| 久久人体大胆视频| 欧美日韩综合另类| 黄色一区二区在线观看| 亚洲精选国产| 久久精品亚洲一区二区| 亚洲二区视频| 午夜精品一区二区三区在线| 老巨人导航500精品| 国产精品成人一区二区网站软件 | 久久婷婷久久一区二区三区| 欧美人与性动交a欧美精品| 国产精品女主播在线观看| …久久精品99久久香蕉国产| 一区二区三区免费网站| 久久三级视频| 99视频在线观看一区三区| 久久久精品日韩欧美| 国产精品扒开腿做爽爽爽软件| 激情婷婷久久| 亚洲欧美一级二级三级| 亚洲电影在线| 久久成人18免费观看| 国产精品超碰97尤物18| 亚洲精品久久久久中文字幕欢迎你|