• <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>
            Fork me on GitHub
            隨筆 - 215  文章 - 13  trackbacks - 0
            <2018年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567


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

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            相冊

            Awesome

            Blog

            Book

            GitHub

            Link

            搜索

            •  

            積分與排名

            • 積分 - 215445
            • 排名 - 118

            最新評論

            閱讀排行榜

            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 思月行云 閱讀(256) 評論(0)  編輯 收藏 引用 所屬分類: Golang
            91精品无码久久久久久五月天| 国产999精品久久久久久| 久久久久这里只有精品| 久久午夜福利电影| 久久精品国产精品亚洲精品| 久久国产欧美日韩精品 | 欧美国产精品久久高清| 国产精品久久久久免费a∨| 久久影院综合精品| 亚洲?V乱码久久精品蜜桃 | 欧美久久天天综合香蕉伊| 中文字幕人妻色偷偷久久| 国产精品免费久久| 色偷偷久久一区二区三区| 久久精品一区二区影院| 久久久久99精品成人片欧美| 亚洲一级Av无码毛片久久精品| 九九久久自然熟的香蕉图片| 精品久久久一二三区| 99久久无码一区人妻| 久久人人爽人人爽人人片AV不| 久久久久亚洲精品天堂久久久久久| 国产成年无码久久久久毛片| 久久久久人妻一区二区三区| 国产巨作麻豆欧美亚洲综合久久| 亚洲欧美伊人久久综合一区二区| 青青草原综合久久大伊人导航 | 女人高潮久久久叫人喷水| 精品久久久无码中文字幕| 久久国产成人精品麻豆| 久久婷婷五月综合97色一本一本| 亚洲色欲久久久综合网| 国内精品久久久久影院亚洲| 亚洲中文字幕伊人久久无码| 蜜桃麻豆www久久国产精品| 欧美国产成人久久精品| 内射无码专区久久亚洲| 四虎国产精品成人免费久久 | 久久精品夜夜夜夜夜久久| 偷偷做久久久久网站| 新狼窝色AV性久久久久久|