Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何在Golang模板中打印新行?_Html_Mysql_Go_Go Templates - Fatal编程技术网

Html 如何在Golang模板中打印新行?

Html 如何在Golang模板中打印新行?,html,mysql,go,go-templates,Html,Mysql,Go,Go Templates,我在MySQL中存储了一些类似这样的内容 "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com" 当我在Golang模板中打印它时,它的解析不正确。我是说所有的东西都显示在一行中 应该是这样印刷的 Hi! How are you? Here is the link you wanted: http://www.google.com 这是我的模板代码 <tr> <td>Te

我在MySQL中存储了一些类似这样的内容

"Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"
当我在Golang模板中打印它时,它的解析不正确。我是说所有的东西都显示在一行中

应该是这样印刷的

Hi!
How are you?
Here is the link you wanted:
http://www.google.com
这是我的模板代码

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

正文
{{.Data.Content}
我错过什么了吗

在这里,您可以使用函数解析字符串,并使用
sep
作为分隔符将子字符串拆分为多个片段

package main

import (
    "fmt"
    "strings"
)

func main() {
    txt := "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.google.com"
    res := strings.Split(txt, "\n")
    for _, val := range res {
        fmt.Println(val)
    }
}
输出将是:

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

示例。

若要在浏览器中打印此内容,请将
\n
替换为,例如


类似于
body=strings。替换(body,“\n”、“
”、-1)

请参阅此工作示例代码:

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!\nHow are you?\nHere is the link you wanted:\nhttp://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, "\n", "<br>", -1)
    fmt.Fprint(w, body)
}

type DataContent struct {
    Data, Content string
}
主程序包
进口(
“字节”
“fmt”
“html/模板”
“日志”
“net/http”
“字符串”
)
func main(){
http.HandleFunc(“/”,ServeHTTP)
如果err:=http.ListenAndServe(“:80”,nil);err!=nil{
log.Fatal(错误)
}
}
func ServeHTTP(w http.ResponseWriter,r*http.Request){
html:=`
资料
内容
{{.Data}
{{.Content}
`
st:=“嗨!\n你好吗?\n您想要的链接是:\nhttp://www.google.com"
数据:=数据内容{“数据”,st}
buf:=&bytes.Buffer{}
t:=template.Must(template.New(“template1”).Parse(html))
如果err:=t.Execute(buf,data);err!=nil{
恐慌(错误)
}
正文:=buf.String()
正文=字符串。替换(正文“\n”、“
”、-1) fmt.Fprint(带阀体) } 类型DataContent结构{ 数据、内容字符串 }
要查看输出,请运行此代码并在
http://127.0.0.1/

另见:


我希望这能有所帮助。

谢谢Simo,但我想在浏览器中打印。go中是否已有模板帮助程序?