【问题标题】:Using the private "lowercase" functions from a Go package使用 Go 包中的私有“小写”函数
【发布时间】:2015-02-28 12:31:32
【问题描述】:

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

html.go(来自blackfriday):

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

ma​​in.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

正确的做法是什么?

【问题讨论】:

    标签: go package


    【解决方案1】:

    你不能。这是黑色星期五的作者深思熟虑的决定。你唯一的选择是定义你自己的函数来做同样的事情。这就像在 Java 之类的语言中访问私有成员一样。

    请参阅此处的规范: https://golang.org/ref/spec#Exported_identifiers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 2018-07-05
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多