duanbiyi7319 2016-08-22 11:40 采纳率: 100%
浏览 48
已采纳

如何在Golang中用div包装每行?

I have a text paragraph.

I would like to wrap each line with a div.

For example if the text string looks like this

Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. 

Nam eget lectus porta metus aliquam tincidunt sed vel enim.

Then I would like to wrap it like this

<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>

Can someone tell me what is the proper way?

Thanks

  • 写回答

2条回答 默认 最新

  • dongxie1907 2016-08-22 12:06
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 IED中交流采样通道、以及程序流程的设计
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 GPTs营销指令提示词和创建方案
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法