使用Go包中的私有“lowercase”函数。

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

Using the private "lowercase" functions from a Go package

问题

我正在尝试在我的main包中使用以下函数:

html.go(来自blackfriday):

func doubleSpace(out *bytes.Buffer) {
    if out.Len() > 0 {
        out.WriteByte('\n')
    }
}

main.go:

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

我尝试过使用blackfriday.doubleSpaceblackfriday.DoubleSpace,但在这三种情况下都得到了undefined

正确的方法是什么?

英文:

I'm trying so use the following function in my main package:

html.go (from blackfriday):

func doubleSpace(out *bytes.Buffer) {
	if out.Len() &gt; 0 {
		out.WriteByte(&#39;\n&#39;)
	}
}

main.go:

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

I tried doing blackfriday.doubleSpace and blackfriday.DoubleSpace but in the three cases I get undefined.

What's the correct way to do it?

答案1

得分: 5

你无法这样做。这是blackfriday作者的有意决定。你唯一的选择是定义自己的函数来完成相同的功能。这就像在Java等语言中访问私有成员一样。

在这里查看规范:
https://golang.org/ref/spec#Exported_identifiers

英文:

You can't. That is a deliberate decision by the authors of blackfriday. Your only option is to define your own function which does the same thing. It's just like accessing a private member in a language like Java.

See the spec here:
https://golang.org/ref/spec#Exported_identifiers

huangapple
  • 本文由 发表于 2015年2月28日 12:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/28778062.html
匿名

发表评论

匿名网友

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

确定