簡體   English   中英

轉到:如何在go中使用func()bool參數?

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

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

所以我不知道如何輸出實際的文本( This is a being rendered in a custom way. )。 有任何想法嗎?

這是該函數的源代碼:

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

我相信您要做的就是致電text

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

我認為您使用的功能是錯誤的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM