如何在Go中使用带有`func() bool`参数的函数?

huangapple go评论92阅读模式
英文:

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")
}
英文:

This is a sample code from the Go blackfriday package:

package main

import (
	&quot;bytes&quot;
	&quot;fmt&quot;
	&quot;github.com/russross/blackfriday&quot;
)

func main() {

input := []byte(`##Title

- another paragragh

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

	htmlFlags := 0
	renderer := &amp;renderer{Html: blackfriday.HtmlRenderer(htmlFlags, &quot;&quot;, &quot;&quot;).(*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, &quot;&lt;custom link %q to %q&gt;&quot;, 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, &quot;&lt;p class=&#39;custom&#39;&gt;%q&lt;/p&gt;&quot;, text)
}

But the output was this:

&lt;h1&gt;Title&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;another paragragh&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&#39;custom&#39;&gt;%!q(func() bool=0x80a15d0)&lt;/p&gt;

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(&quot;&lt;p&gt;&quot;)
	if !text() {
		out.Truncate(marker)
		return
	}
	out.WriteString(&quot;&lt;/p&gt;\n&quot;)
}

答案1

得分: 1

我相信你只需要调用text函数。

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

请注意,这是一段Go语言代码,用于在输出缓冲区中生成一个带有自定义类的段落标签。%q是一个占位符,用于将text()函数的结果插入到生成的HTML中。

英文:

I believe all you need to do is call text

func (options *renderer) Paragraph(out *bytes.Buffer, text func() bool) {
    fmt.Fprintf(out, &quot;&lt;p class=&#39;custom&#39;&gt;%q&lt;/p&gt;\n&quot;, text())
}

答案2

得分: 1

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

看一下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")
}
英文:

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(&quot;&lt;p class=&#39;custom&#39;&gt;&quot;)
	if !text() {
		out.Truncate(marker)
		return
	}
	out.WriteString(&quot;&lt;/p&gt;\n&quot;)
}

huangapple
  • 本文由 发表于 2015年2月27日 14:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/28758686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定