is it possible to implement an interface that has private methods?

5,930 views
Skip to first unread message

Octavian Costache

unread,
Jul 4, 2013, 6:05:14 PM7/4/13
to golan...@googlegroups.com
Hi everybody,


TL;DR:
Does the presence of a private method on an interface make it impossible to implement that interface outside the package in which it is defined?


Longer story:
This question is not about Gorp, but about implementing an interface that specifies private methods.

We're using Gorp (https://github.com/coopernurse/gorp) as an ORM and so far it's been working out.
But I've gotten to a point where I want to add unittests, and I want to mock out some Gorp functionality.

More precisely I'm trying to create a mock that implements the gorp.SqlExecutor interface which looks kind of like this:

type SqlExecutor interface {
  Get(i interface{}, keys ...interface{}) (interface{}, error)
  Insert(list ...interface{}) error
  Update(list ...interface{}) (int64, error)
  Delete(list ...interface{}) (int64, error)
 
  query(query string, args ...interface{}) (*sql.Rows, error) 
  queryRow(query string, args ...interface{}) *sql.Row
}
 
Notice  the two private methods at the bottom of the interface.

If I try to create a type MockSqlExecutor that implements all the methods in this interface, I get the following error:

go/src/jello/db/db.go:50: cannot use MockSqlExecutor literal (type *MockSqlExecutor) as type gorp.SqlExecutor in assignment:
  *MockSqlExecutor does not implement gorp.SqlExecutor (missing gorp.query method)

Does the presence of a private method on an interface make it impossible to implement that interface outside the package in which it is defined?
Am I missing something here? 

Thanks!
Octavian.

Dan Kortschak

unread,
Jul 4, 2013, 6:19:44 PM7/4/13
to Octavian Costache, golan...@googlegroups.com
Yes, unless you embed the interface in a struct you define.
Message has been deleted

Octavian Costache

unread,
Jul 4, 2013, 6:31:03 PM7/4/13
to Islan Dberry, golang-nuts
Awesome, that's exactly what I was missing! 
Thanks guys!


On Thu, Jul 4, 2013 at 6:25 PM, Islan Dberry <islan...@ymail.com> wrote:



Does the presence of a private method on an interface make it impossible to implement that interface outside the package in which it is defined?

It is possible to implement the interface outside of the package by embedding the interrace. You can mock the exported methods. 

type MockQqlExecutor struct {
   gorp.SqlEecutor
}

func (m *MockSqlExecutor)  Get(i interface{}, keys ...interface{}) (interface{}, error)  { /* your mock impl here */ }
func (m *MockSqlExecutor)  Insert(list ...interface{}) error { /* your mock impl here */ }
func (m *MockSqlExecutor)  Update(list ...interface{})  { /* your mock impl here */ }
func (m *MockSqlExecutor)  Delete(list ...interface{})  { /* your mock impl here */ }




--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/6hpUErAfMHI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Octavian Costache · @okvivi

Reply all
Reply to author
Forward
0 new messages