Wednesday, June 8, 2011

Summary of Object Oriented Programming Goodness in Go, without the pain

If you haven’t gone over some of the related topics on this, please do so now. Here I’ll mostly be summarizing what we already went over so that it is available as a quick reference, especially for all those people who have been used to doing object oriented programming.

  • Structs in Go

  • Anonymous fields in structs

  • Methods on structs

  • Inheritance and subclassing

  • Interfaces in Go

  • Polymorphism in Go

  • The basic tenets of objected oriented programming are encapsulation, inheritance, and polymorphism. They are not exclusive to OOPs though. Go allows you get the benefits of those important ideas without having to deal with the complexity of OOPs. It must be stressed again that Go is NOT object oriented. So you might have to realign your thinking a bit but it is worth it. It is to be remembered that it is not an exact match between Go concepts and OOPs concepts. So even if I’m likening the two, it is only for the sake of simplicity of understanding and learning.

    Encapsulation: the ability to restrict or provide access to data and the ability to tie behavior or methods with the data. For Go some of the salient features are:
    * There are two levels of access - within the package alone, and public.
    * If a field, type, or method starts with a capital letter it is exported outside the package and is public. If instead it starts with a small letter, it is visible only within the package.
    * Exported/public items: MyStruct, MyMethod, MyField
    * Items with package visibility: myStruct, myMethod, myField
    * You can tie in methods/behavior to a type by defining functions associated with it. func (m my_type) my_func() int { }
    * You cannot attach methods to a type if it is not defined in the local package.

    Inheritance: the ability for one type to obtain the features of a type above it in a hierarchy. For Go, some of the salient features are:
    * Inheritance is obtained through anonymous fields - anonymous fields appear to attach its behavior to the composing class.
    * Both data fields and methods are available to derived types. Outside of the package, only the types, fields, and methods named with a starting capital letter is inherited. Inside a package, everything is inherited.
    * Multiple inheritance is possible - by including an anonymous field of each of the parent types. type Child struct { Father; Mother }

    Polymorphism: when a type seems to exhibit different behaviors when linked to different instances, the type can be said to exhibit polymorphism.
    * Interfaces in Go can be used to implement polymorphism. A variable of a type can be assigned to a variable of any interface it implements.


    1. Your notes on Go, in particular how OOP concepts translate, are excellent. Thanks for taking the time.

    2. Explanation id good. Please try to illustrate the explanations with some examples. That would be more useful.

    3. These tutorials are very succinct and clear. Very helpful. Thank you for writing these.

    4. Thanks for these tutorial. You are nice. X-Factor Diet System Review

    5. Thanks for showing this to us. I will take it as best advice. The Venus factor

    6. Thanks for sharing this story. I really love this. Old School New Body Plan

    7. This is my favorite tutorial. I love it. Visit Here


    If you think others also will find these tutorials useful, kindly "+1" it above and mention the link in your own blogs, responses, and entries on the net so that others also may reach here. Thank you.

    Note: Only a member of this blog may post a comment.