How does one implement an interface but forbid users from calling the functions that implement that interface?
For example, we have a module that implements some interface I
which has a required function to implement Bar
:
//mymodule.go
import (I)
type Foo struct {
}
func (f *Foo) Bar(
...
// DONT want users calling this directly
// I.Bar eventually calls this
) // dictated by I
func (f *Foo) BarCallMe() {
...
I.Bar(f)
}
F = Foo{}
F.Bar() // make this not possible, do not want!
F.BarCallMe() // $PROFIT
How do we prevent a user of mymodule from using Bar()
directly like f.Bar()
? Meaning, if we want users of this module to only call BarCallMe
how do we hide Bar
?
More generically, how do you implement one interface but publish another?
Concrete example: Recently I implemented golang's heap interface which requires Push
and Pop
be implemented by the implementor. However, heap.Push
calls your Push
(https://golang.org/src/container/heap/heap.go?s=1949:1986#L42), so if a user called my Push
directly, it would not have the intended result. So the question here is how to publish a Push
that users call, while still providing a Push
that meets the interface reqs.