英文:
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.doubleSpace
和blackfriday.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() > 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")
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论