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

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在线| 亚洲人体影院| 国产精品蜜臀在线观看| 久久伊人亚洲| 欧美裸体一区二区三区| 欧美一区精品| 欧美电影在线播放| 欧美一区二区播放| 免费成人性网站| 午夜精品在线视频| 免费亚洲电影在线| 亚洲欧美欧美一区二区三区| 久久精品国产亚洲精品| 艳女tv在线观看国产一区| 亚洲欧美在线看| 亚洲另类自拍| 先锋影音久久| 一区二区三区导航| 久久亚洲国产成人| 亚洲欧美国产77777| 麻豆av一区二区三区久久| 亚洲免费网址| 欧美高清在线精品一区| 久久精品99| 欧美日本国产视频| 欧美大片18| 国产婷婷一区二区| 99亚洲视频| 亚洲黑丝在线| 久久精品中文字幕一区二区三区| 亚洲午夜女主播在线直播| 乱中年女人伦av一区二区| 香港成人在线视频| 欧美手机在线视频| 亚洲黄色毛片| 亚洲黄色一区| 久久久欧美精品sm网站| 久久gogo国模啪啪人体图| 欧美偷拍一区二区| 亚洲激情婷婷| 亚洲精品乱码久久久久久日本蜜臀 | 玖玖玖国产精品| 亚洲欧美制服中文字幕| 欧美激情精品久久久久| 欧美国产日韩一区二区| 精品88久久久久88久久久| 午夜日韩激情| 久久精品国产一区二区电影 | 欧美激情在线狂野欧美精品| 久久久久久久久综合| 国产麻豆视频精品| 亚洲一区中文字幕在线观看| 亚洲免费在线| 国产精品久在线观看| 亚洲午夜av在线| 亚洲欧美一区二区视频| 国产精品日韩在线观看| 亚洲一区二区伦理| 久久岛国电影| 精品av久久707| 狂野欧美一区| 亚洲精品在线免费| 亚洲综合视频在线| 国产欧美一区二区三区在线老狼| 亚洲综合大片69999| 久久久国产午夜精品| 国内精品久久久| 看片网站欧美日韩| 亚洲黄色av| 亚洲女ⅴideoshd黑人| 国产伦精品一区二区三区免费| 欧美一级片一区| 免费观看亚洲视频大全| 亚洲精品欧美日韩| 国产精品色一区二区三区| 欧美一级专区| 亚洲大片免费看| 亚洲欧美国产一区二区三区| 国产一区日韩二区欧美三区| 久久尤物电影视频在线观看| 亚洲精品乱码久久久久久蜜桃麻豆| 一区二区三区欧美日韩| 国产欧美日韩综合精品二区| 久久精品99国产精品日本| 91久久视频| 欧美在线观看网址综合| 亚洲欧洲视频| 国产欧美欧洲在线观看| 美女视频一区免费观看| 亚洲一区二区三区四区五区午夜| 久久久精品一区| 一区二区激情视频| 国内精品久久久久久久果冻传媒| 欧美高清你懂得| 欧美在线视频播放| 一本大道久久a久久精品综合| 久久国产福利| 亚洲小说欧美另类婷婷| 激情成人av| 国产精品一区二区在线观看| 欧美高清在线精品一区| 午夜亚洲伦理| 国产精品99久久久久久人 | 一区二区免费在线视频| 国内精品久久久久影院 日本资源 国内精品久久久久伊人av | 亚洲精品国产拍免费91在线| 国产精品亚洲片夜色在线| 欧美激情亚洲激情| 久久精品一二三区| 亚洲欧美日韩国产成人| 亚洲精选一区| 亚洲国产精品福利| 久久综合一区二区三区| 欧美一区二区视频免费观看| 亚洲视频中文字幕| 日韩视频在线观看免费| 亚洲高清影视| 在线观看亚洲精品视频| 国产欧美日本一区视频| 国产精品va在线| 欧美日韩一区精品| 欧美日韩国产首页| 欧美黄色一级视频| 欧美国产在线电影| 蜜臀久久99精品久久久久久9 | 久久一区二区三区超碰国产精品| 午夜免费在线观看精品视频| 亚洲视频欧美在线| 在线中文字幕一区| 亚洲一区二区三区四区视频| av成人福利| 亚洲一区久久| 午夜精品理论片| 欧美一区二视频| 久久国产欧美精品| 久久久久国产精品麻豆ai换脸| 性视频1819p久久| 久久成人亚洲| 久久亚洲图片| 欧美激情国产精品| 欧美日韩国产另类不卡| 国产精品v亚洲精品v日韩精品| 国产精品国产三级欧美二区| 国产精品毛片在线| 国产亚洲va综合人人澡精品| 国产视频不卡| 亚洲国产1区| 99视频国产精品免费观看| 亚洲一卡二卡三卡四卡五卡| 亚洲一二三区视频在线观看| 亚洲欧美影音先锋| 久久久人成影片一区二区三区| 农村妇女精品| 99re热精品| 性欧美xxxx大乳国产app| 久久一区二区三区四区| 欧美激情综合五月色丁香小说| 欧美日韩国产影院| 国产午夜精品理论片a级探花| 亚洲第一主播视频| 亚洲天堂av电影| 久久女同互慰一区二区三区| 亚洲观看高清完整版在线观看| 亚洲精品欧美专区| 欧美主播一区二区三区| 欧美ed2k| 国产深夜精品| 日韩亚洲在线观看| 久久精品国产欧美激情| 亚洲国产日韩欧美在线图片| 亚洲一级高清| 欧美国产亚洲视频| 国产综合激情| 亚洲性夜色噜噜噜7777| 免费影视亚洲| 亚洲影音先锋| 欧美日韩免费观看一区| 黄色另类av| 午夜精品久久久久久99热软件| 欧美不卡福利| 欧美有码在线视频| 欧美小视频在线| 亚洲黄一区二区| 久久久久国产精品麻豆ai换脸| 亚洲理论在线| 免费观看在线综合| 黄色成人精品网站| 欧美一区二区私人影院日本| 亚洲精品偷拍| 欧美成人在线免费观看|