go - Golang implementing database funcs using interfaces -


sorry bit dummy questions, i'm kinda stuck.

so, i'm implementing wrapper above database driver application, , need keep portable possible. came decision interfaces perfect match task. so, have database struct variables , app-specific methods, , 2 interface functions:

query(request string) error flush() int? string?? struct?? slice????, error 

now got main question. how do return data type "flush()" doesn't know? can return interface, , if can, how work that?

second question pretty basic, still isn't clear me. so, have database struct 2 methods designed implemented package user use db driver want.
ho write , how future implementation (there example on tour of go, it's interface different structs similar methods)
hope you'll me find understanding :)

yes, flush can have signiture;

flush() interface{}, error 

how implement? reasonable method bodies should you;

type mydbdriver struct {      //fields }  func (d *mydbdriver) query(request string) error {      return nil }  func (d *mydbdriver) flush() interface{}, error {       return nil, nil } 

in go interface implementations implicit, meaning, if type has methods match interfaces signature, you've implemented it. no need public class mytype: imyinterface, ithisisntcsharp. note in example above *mydbdriver has implemented interface mydbdriver has not.

edit: below pseudo calling code;

e := driver.query("select * thattable thatproperty = 'thatvalue'") if e != nil {     return nil, e }  i, err := driver.flush() if err != nil {      return nil, err }  myconcreteinstance := i.(myconcretetype) // note panic if type of not myconcretetype // can avoided familiar object, err calling syntax myconcreteintance, ok := i.(myconcretetype) if !ok {       // type of not myconcretetype :\ } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -