Go:如何在 Go 中使用 func() bool 参数?

标签 go markdown

这是来自 Go blackfriday 的示例代码包裹:

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

代码的目的是自定义输出 HTML 的 link 属性。

我尝试使用 p 标签来做同样的事情:

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

但是输出是这样的:

<h1>Title</h1>

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

所以我不知道如何输出实际的文本(这是以自定义方式呈现的。)。有什么想法吗?

这是函数的源代码:

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>\n")
}

最佳答案

我认为你对函数的使用是错误的。

看看Html.Paragraph并覆盖它。

也许它看起来像:

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>\n")
}

关于Go:如何在 Go 中使用 func() bool 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758686/

相关文章:

go - 如何解码 Golang Viper snake_case 值

go - 如何将 channel 作为参数传递给函数?

go - 如何获取 Kubernetes 上 pod 的真实和实际存储使用情况?

c# - 为什么 MarkdownSharp 不对我的 HTML 进行编码?

javascript - 摊牌 Markdown 编辑器 : How to resize images to fit in with the content?

我可以使用 & 运算符在函数中获取有效指针,即使该函数已在 golang 中返回

function - 在 Go 条件模板中使用自定义函数

r - 如何在 R Markdown 中全局使用 set.seed()?

syntax - 如何使用markdown语法编写包含右括号的链接?

markdown - 在 Astro 中将 markdown 转换为 HTML