You may use bytes.Buffer
and bufio.NewScanner
, like this working sample code:
package main
import "fmt"
import "strings"
import "bufio"
import "bytes"
func main() {
r := strings.NewReader(`Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nam eget lectus porta metus aliquam tincidunt sed vel enim.`)
buf := &bytes.Buffer{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := scanner.Text()
if len(s) == 0 {
s = `<br/>`
}
buf.WriteString(`<div>` + s + `</div>
`)
}
fmt.Println(buf.String())
}
output:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
You may use bytes.Buffer
and html/template
,
but you have <div></div>
instead of <div><br/></div>
, then you may use
s = strings.Replace(s, `<div></div>`, `<div><br/></div>`, -1)
like this working sample code:
package main
import "fmt"
import "bytes"
import "html/template"
import "strings"
func main() {
r := []string{`Lorem ipsum dolor sit amet,`,
`consectetur adipiscing elit.`,
``,
`Nam eget lectus porta metus aliquam tincidunt sed vel enim.`}
buf := &bytes.Buffer{}
t := template.Must(template.New("template").Parse("{{range .}}<div>{{.}}</div>
{{end}}"))
t.Execute(buf, r)
s := buf.String()
s = strings.Replace(s, `<div></div>`, `<div><br/></div>`, -1)
fmt.Println(s)
}
output:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>