dongyi2534 2016-08-06 12:26
浏览 730
已采纳

如何在Golang模板中打印换行? [重复]

This question already has an answer here:

I have stored some content in MySQL that looks like this.

"Hi!
How are you?
Here is the link you wanted:
http://www.google.com"

When I print it in Golang template, its not parsing correctly. I mean everything displayed in one line.

Its supposed to print like this

Hi!
How are you?
Here is the link you wanted:
http://www.google.com

Here is my template code.

<tr>
    <td>TextBody</td>
    <td>{{.Data.Content}}</td>
</tr>

Am I missing something?

</div>
  • 写回答

2条回答 默认 最新

  • dotcraq3249 2016-08-06 13:49
    关注

    To print this in browser, replace with e.g. <br>
    like body = strings.Replace(body, " ", "<br>", -1)
    See this working sample code:

    package main
    
    import (
        "bytes"
        "fmt"
        "html/template"
        "log"
        "net/http"
        "strings"
    )
    
    func main() {
        http.HandleFunc("/", ServeHTTP)
        if err := http.ListenAndServe(":80", nil); err != nil {
            log.Fatal(err)
        }
    }
    
    func ServeHTTP(w http.ResponseWriter, r *http.Request) {
        html := `
    <!DOCTYPE html>
    <html>
    <body>  
    <table style="width:100%">
      <tr>
        <th>Data</th>
        <th>Content</th> 
      </tr> 
      <tr>
        <td>{{.Data}}</td>
        <td>{{.Content}}</td>
      </tr>
    </table> 
    </body>
    </html>
    `
        st := "Hi!
    How are you?
    Here is the link you wanted:
    http://www.google.com"
        data := DataContent{"data", st}
    
        buf := &bytes.Buffer{}
        t := template.Must(template.New("template1").Parse(html))
        if err := t.Execute(buf, data); err != nil {
            panic(err)
        }
        body := buf.String()
        body = strings.Replace(body, "
    ", "<br>", -1)
        fmt.Fprint(w, body)
    }
    
    type DataContent struct {
        Data, Content string
    }
    

    To see the output, run this code and open your browser at http://127.0.0.1/

    Also see: html/templates - Replacing newlines with <br>

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dqk94069 2016-08-06 13:07
    关注

    Here you can use the Split function to parse the string and split the substring into slices using the sep as separator.

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        txt := "Hi!
    How are you?
    Here is the link you wanted:
    http://www.google.com"
        res := strings.Split(txt, "
    ")
        for _, val := range res {
            fmt.Println(val)
        }
    }
    

    The output will be:

    Hi!
    How are you?
    Here is the link you wanted:
    http://www.google.com
    

    Example on Go Playground.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 div editable中的光标问题
  • ¥15 mysql报错1415Not allowed to return a result set from a trigger 不知如何修改
  • ¥60 Python输出Excel数据整理,算法较为复杂
  • ¥15 回答几个问题 关于数据库
  • ¥15 51单片机串口通信问题,未完成且要修改
  • ¥15 百鸡问题 c++编程问题(相关搜索:输出数据)
  • ¥30 如何在CMD中设置代理
  • ¥15 我有一块薛定谔的硬盘
  • ¥15 微信小游戏开发2D碰撞检测问题
  • ¥30 MapReduce案例实践(实验过程需要截图加文字)