Search code examples
goencapsulation

golang public method to private struct - is this have any use case


I am new to go. I noticed in some libraries there are public methods to a private struct. See for example https://github.com/btcsuite/btcd/blob/master/txscript/stack.go

Does this have a real use case? I cannot picture how I will be able to use public methods, if I cannot access the structure they belong too.

As the following question - is there any way to overcome the private declaration of a struct? For example, I would like to use the structure stack from the pointed file in my package.


Solution

  • So, the major use case for this is that you can return an unexported type from a package function. Any exported methods can then be used on this type, even though other packages can't actually create the type in the first place (except for receiving it as a return value from your package).

    package foobar
    
    type foo struct {}
    func (f foo) Bar() {}
    func Newfoo() foo {
        return foo{}
    }
    ----
    package main
    func main() {
        f := foobar.Newfoo()
        f.Bar()
    
        foobar.NewFoo().Bar()
    }
    

    The other major use case is in package-level interfaces. I tend to use a fairly standard pattern wherein a package defines an exported interface, like this:

    type Doer interface {
        Do()
    }
    

    And then defines some private type(s) that fulfills that interface:

    type stringDoer struct {}
    
    func (s *stringDoer) Do() {}
    
    func NewStringDoer() Doer {
        return &stringDoer{}
    }
    

    That way external packages can use my types as interfaces without having to mess around with concrete types, and since the exposed data types of my package are entirely interfaces, it makes mocking or stubbing calls to my package (example, for testing) incredibly easy.

    For this type of system to work, the methods on the struct (in this case, stringDoer) must be exported, even if the struct itself isn't.