Can someone give me a concrete example to this advise ?

https://github.com/golang/go/wiki/CodeReviewComments#interfaces

So is this how i follow it ?

package worker

type interface DB {
  getAllTable()
}


type worker struct {
  db DB
}

func (w worker) doSomething() {
  w.db.getAllTable() 
}

package listener

type interface DB {
  getAllTable()
}

type listener struct {
  db DB
}

func (l listener) doSomething() {
  l.db.getAllTable() 
}

package msql

type mysql struct {}

func (mysql) getAllTable() {
}

Is it alright to duplicate the interfaces in 2 places to satisfy ‘define consumer where it is consumed’.


What about using interface to show generality ? It’s defining interface not where it is consumed

For example hash/crc32 and hash/adler32 implementing hash.Hash32

1

There are 1 best solutions below

0
On

You could move the DB interface in to a different package, then the worker and listener packages could both use it.