• <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
            <2016年3月>
            282912345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789


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

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            相冊(cè)

            Awesome

            Blog

            Book

            GitHub

            Link

            搜索

            •  

            積分與排名

            • 積分 - 216792
            • 排名 - 118

            最新評(píng)論

            閱讀排行榜

            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 思月行云 閱讀(258) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Golang
            无码精品久久久天天影视| 久久久精品免费国产四虎| 久久婷婷人人澡人人| 日本精品久久久久久久久免费| 久久亚洲AV成人无码软件| 久久99精品国产麻豆| 久久亚洲欧洲国产综合| 久久ww精品w免费人成| 久久人人爽人爽人人爽av| 天堂久久天堂AV色综合| 久久精品国产一区二区三区 | 国内精品综合久久久40p| AV无码久久久久不卡网站下载| 欧洲性大片xxxxx久久久| 久久精品草草草| 久久丫精品国产亚洲av不卡 | 国产精品日韩深夜福利久久| 精品人妻伦九区久久AAA片69| 亚洲精品高清国产一久久| 97精品依人久久久大香线蕉97| 久久精品国产色蜜蜜麻豆| 狠色狠色狠狠色综合久久| 色综合久久无码五十路人妻| 亚洲第一永久AV网站久久精品男人的天堂AV | 草草久久久无码国产专区| 欧美精品九九99久久在观看| 99久久精品国产一区二区三区| 国内精品久久久久影院优| 一本色综合网久久| 亚洲精品综合久久| 久久国产成人午夜aⅴ影院| 欧美777精品久久久久网| 国产成人综合久久综合| www久久久天天com| 97久久精品国产精品青草| 狠狠色婷婷久久一区二区三区| 欧美va久久久噜噜噜久久| 精品久久久久香蕉网| 91精品国产综合久久久久久| 久久精品国产亚洲欧美| 国产精品久久久99|