简体   繁体   English

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

[英]Go: How to use the func() bool arguments in go?

This is a sample code from the Go blackfriday package: 这是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)
}

The purpose of the code is to customize the link attributes of the output HTML. 该代码的目的是自定义输出HTML的link属性。

I tried to do the same but with p tags: 我试图做同样的事情,但带有p标签:

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. ). 所以我不知道如何输出实际的文本( 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>\n")
}

I believe all you need to do is call text 我相信您要做的就是致电text

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

I think your usage of function is wrong. 我认为您使用的功能是错误的。

Take a look at Html.Paragraph and override this. 看看Html.Paragraph并覆盖它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM