Thanks for the replies. Sincerely, Arafath M
On Fri, Oct 28, 2016 at 6:25 AM, Henry <henry.adisuma...@gmail.com> wrote: > Hi, > > I usually use the following pattern when creating a type. In your case, it > would be as follows: > > interface Animal { > Eat() > Travel() > } > > interface Mammal { > Animal > NoOfLegs() int > } > > //Note this is unexported > struct mammalImpl { > //implementation here > } > > //Mammal constructor. This will not compile if mammalImpl does not satisfy > Mammal. > func NewMammal() Mammal { > return new(mammalImpl) > } > > func (m mammalImpl) Eat() { > //implementation here.. > } > > func (m mammalImpl) Travel() { > //implementation here.. > } > > func (m mammalImpl) NoOfLegs() { > ///implementation here.. > } > > What I like with the above pattern is that it forces the users of my API > to rely upon abstraction, which allows me certain flexibility for code > changes in the future. In addition, if I want to have an overview what > Mammal does, all I need to do is to look at the Mammal interface, which is > a lot shorter and easier to see than inspecting its actual implementation. > If I need to change Mammal, I will just change the Mammal interface and the > compiler will let me know what are its implementation that also need to be > changed. So far, this pattern has been very helpful. > > > On Thursday, October 27, 2016 at 2:11:04 PM UTC+7, Arafath M wrote: > >> Hi, >> >> Using interface is easy in Go. But, while studying an existing code >> base, it is very difficult to find which types have implementation of a >> particular interface. The situation will become worse, if there are many >> interface functions inside an interface definition. >> >> In another languages, it is easy to just search for the class, which >> implements a particular interface. >> >> For example in Java, >> >> interface Animal { >> public void eat(); >> public void travel(); >> } >> >> public class MammalInt implements Animal { >> >> public void eat() { >> System.out.println("Mammal eats"); >> } >> >> public void travel() { >> System.out.println("Mammal travels"); >> } >> >> public int noOfLegs() { >> return 0; >> } >> >> public static void main(String args[]) { >> MammalInt m = new MammalInt(); >> m.eat(); >> m.travel(); >> } >> } >> >> By seeing the above line "*public class MammalInt **implements Animal*" one >> can easily understand that Animal interface is implemented in MammalInt >> >> In Go, as far as I know, I need to search for all interface functions to >> find whether a type implements a particular interface or not. >> >> Have anybody else experienced the same? If yes, what is the solution? >> >> *Note:* Personally I like to write code in Go; all of my recent projects >> are in Go. I prefer Go than Java. >> >> Sincerely, >> Arafath M >> > -- > 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. > -- 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.