Thank you Alex for your quick reply. I figured it out. Using Interfaces is the right way to solve this.
On Friday, 25 May 2018 12:08:01 UTC-7, alex....@gmail.com wrote: > > This is what interfaces are for. > > You have: > type GetKeyer interface { > GetKey1() string > GetKey2() string > } > > Then to use it: https://play.golang.org/p/rvLsWCIBuxe > You still have to implement GetKey1 and GetKey2 on each struct, but you > can access them through a common interface without knowing the actual type. > > On Saturday, 26 May 2018 02:23:39 UTC+8, anil kuncham wrote: >> >> Hi, >> >> I have a use case where there are two different versions of struct. The >> problem is when the model is retrieved from database, it can be of either >> version. Both structs have same getters implemented. >> This is what the code looks like. >> >> type structA { >> Key1 string >> Key2 string >> } >> >> func(s *structA) GetKey1() string { >> return s.key1 >> } >> >> >> type structB { >> Key3 string >> Key4 string >> } >> >> func(s *structB) GetKey1() string { >> return s.key3 >> } >> >> version, obj, err := getObjFromDB(args) >> if err != nil { >> return err >> } >> >> var objA *structA >> var objB *structB >> >> if version == "A" { >> objA := obj.(structA) >> } else if version == "B" { >> objB := obj.(structB) >> } >> >> .... >> I want this logic to be abstracted to one object so that subsequent code >> should like >> >> obj.GetKey() -> doesn't matter whether obj is of type structA or structB >> >> Any help is greatly appreciated. Thanks for your time to look into this. >> >> >> >> >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.