golang public method to private struct - is this have any use case
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.