duaevb1511 2015-05-07 19:08
浏览 54
已采纳

在Go中通过Godoc记录有关私有结构的公共方法

I have a private struct (actually more than one, I simplified a bit for this example) which I have a public function on, that satisfies a public interface. This struct is created via a factory method (NewX-style):

package godocprivate

type PublicInterface interface {
    PublicFunction()
}

type privatestruct struct {
}

func NewPublic() *privatestruct {
    ps := &privatestruct{}
    return ps
}

// PublicFunction does something to be documented
func (self *privatestruct) PublicFunction() {
}

I cannot make the factory method return the public interface, because there are other interfaces to be satisfied by the returned value, in my project.

I do want to document PublicFunction() via godoc, but because it is on a private struct, it does not show up:

This is how Godoc of the above looks like

Is there any way, trick, workaround, etc. to make privatestruct.PublicFunction() visible in Godoc?

My actual use case is even more severe: I do have more than one private struct. All of them satisfy PublicInterface, but the inner workings of their respective PublicFunction-implementations differ, hence could their documentations need to be different, too...

  • 写回答

1条回答 默认 最新

  • dozr13344 2015-05-07 19:29
    关注

    PublicFunction uses PublicStruct whose private fields will be private with private documentation. For example,

    package godocprivate
    
    type PublicInterface interface {
        PublicFunction()
    }
    
    type privatestruct struct {
    }
    
    // PublicStruct is something to be documented except for private fields
    type PublicStruct struct {
        privatestruct
    }
    
    func NewPublic() *PublicStruct {
        ps := &PublicStruct{}
        return ps
    }
    
    // PublicFunction does something to be documented
    func (p *PublicStruct) PublicFunction() {
    }
    

    When PublicStruct is created it initially has zero values for each field. If that's not enough, introduce a factory boolean. For example,

    package godocprivate
    
    type PublicInterface interface {
        PublicFunction()
    }
    
    type privatestruct struct {
    }
    
    // PublicStruct is something to be documented except for private fields
    type PublicStruct struct {
        factory bool
        privatestruct
    }
    
    func NewPublic() *PublicStruct {
        ps := &PublicStruct{factory: true}
        return ps
    }
    
    // PublicFunction does something to be documented
    func (p *PublicStruct) PublicFunction() {
        if !p.factory {
            panic("Use NewPublic")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集