Reputation: 18524
Use case
I am using Shopify's sarama libary which is a client library. More specifically I am using the ClusterAdmin
struct which is great, however I want to add another method ListDetailedTopics
to it and I must call ClusterAdmin's non public methods.
https://github.com/Shopify/sarama/blob/master/admin.go
Problem
I am not sure how I could "extend" the exported struct by my own method. I tried this:
func (ca *sarama.ClusterAdmin) ListDetailedtopics() {
b, err := ca.findAnyBroker() // This is a private method I need to call
}
That did not work because "invalid receiver type *sarama.ClusterAdmin (sarama.ClusterAdmin is an interface type)". How can I extend the struct/interface by my own methods?
Upvotes: 1
Views: 418
Reputation: 4662
You cannot extend the interface outside its package.
What you can do instead is define a new interface that implements the old interface and then add your new method to the new interface. For example:
type ExtendedClusterAdmin interface {
sarama.ClusterAdmin
ListDetailedtopics()
}
If you want to use private function within the package then you would have to do it inside the package itself. For example, with a patch.
Upvotes: 5