How to print new lines in Golang template?
13,536
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!\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)
}
}
The output will be:
Hi!
How are you?
Here is the link you wanted:
http://www.google.com
Example on Go Playground.
Author by
Giri
Updated on June 27, 2022Comments
-
Giri almost 2 years
I have stored some content in MySQL that looks like this.
"Hi!\nHow are you?\nHere is the link you wanted:\nhttp://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?
-
Giri over 7 yearsThanks Simo, but i want to print this in browser. Is there any template helpers already available in go?