브라우저에서 인쇄하려면 \n
을 예 : html/templates - Replacing newlines with <br>
을 난이 도움이되기를 바랍니다 : 볼이 코드를 실행하고 또한 http://127.0.0.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
}
출력을 보려면 : body = strings.Replace(body, "\n", "<br>", -1)
같은 <br>
이 작업 샘플 코드를 참조하십시오 .
감사합니다. Simo,하지만 브라우저에서 인쇄하고 싶습니다. 이미 템플릿 헬퍼가 있습니까? – user1091558