dsg435665475 2015-02-27 06:13
浏览 109
已采纳

转到:如何在go中使用func()bool参数?

This is a sample code from the Go blackfriday package:

package main

import (
    "bytes"
    "fmt"
    "github.com/russross/blackfriday"
)

func main() {

input := []byte(`##Title

- another paragragh

This is a being rendered in a custom way.
`)

    htmlFlags := 0
    renderer := &renderer{Html: blackfriday.HtmlRenderer(htmlFlags, "", "").(*blackfriday.Html)}

    extensions := 0

    unsanitized := blackfriday.Markdown(input, renderer, extensions)
    os.Stdout.Write(unsanitized)
}

// renderer implements blackfriday.Renderer and reuses blackfriday.Html for the most part,
// except overriding Link rendering.
type renderer struct {
    *blackfriday.Html
}

func (options *renderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {
    fmt.Fprintf(out, "<custom link %q to %q>", content, link)
}

The purpose of the code is to customize the link attributes of the output HTML.

I tried to do the same but with p tags:

func (options *renderer) Paragraph(out *bytes.Buffer, text func() bool) {
    fmt.Fprintf(out, "<p class='custom'>%q</p>", text)
}

But the output was this:

<h1>Title</h1>

<ul>
<li>another paragragh</li>
</ul>
<p class='custom'>%!q(func() bool=0x80a15d0)</p>

So I have no idea how to output the actual text (This is a being rendered in a custom way.). Any ideas?

This is the source code of the function:

func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) {
    marker := out.Len()
    doubleSpace(out)

    out.WriteString("<p>")
    if !text() {
        out.Truncate(marker)
        return
    }
    out.WriteString("</p>
")
}
  • 写回答

2条回答 默认 最新

  • dte66654 2015-02-27 07:53
    关注

    I think your usage of function is wrong.

    Take a look at Html.Paragraph and override this.

    Maybe it would look like:

    func (options *renderer) Paragraph(out *bytes.Buffer, text func() bool) {
        marker := out.Len()
        doubleSpace(out) 
    
        out.WriteString("<p class='custom'>")
        if !text() {
            out.Truncate(marker)
            return
        }
        out.WriteString("</p>
    ")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办