$30 off During Our Annual Pro Sale. View Details »

Object Oriented Programming in Go

Object Oriented Programming in Go

Talk given at BostonGolang (bostongolang.org) to an audience of programmers looking to transition to Go from other OO languages. This gives a good overview of how Go does OO differently and how to do it idiomatically.

Johnny Boursiquot

February 24, 2015
Tweet

More Decks by Johnny Boursiquot

Other Decks in Technology

Transcript

  1. OOP in Go
    Familiar but Different

    View Slide

  2. About Me
    Johnny Boursiquot
    Principal Software Engineer 

    @ RBM T
    echnologies
    @jboursiquot on T
    witter, GitHub, etc
    Programing language enthusiast

    View Slide

  3. About this talk
    Beginner to Intermediate level
    Basic understanding of OOP from another
    language for context
    Some familiarity with Go syntax expected but
    not mandatory

    View Slide

  4. What we’ll cover
    What is OOP anyway?
    How does Go differ?
    What does OOP look and feel like in Go?
    Q&A

    View Slide

  5. What is OOP anyway?

    View Slide

  6. OOP
    OOP tends to model concepts as “objects”
    that interact with other objects within the world
    of your program
    Most are familiar with class-based OOP
    Java, C++, Objective-C, Ruby, Python and
    others, all embrace class-based OO

    View Slide

  7. OOP
    “Objects” have both data (state) and behavior
    (code, often called “methods”)
    Behaviors access and often modify object state
    Objects tend to have the notion of “self” or
    “this”

    View Slide

  8. OOP
    “Objects” represent “instances” of “classes” in
    common OO languages
    Class-based orientation commonly includes
    Inheritance and Polymorphism
    OO programmers have come to favor
    composition over inheritance

    View Slide

  9. How does Go differ?

    View Slide

  10. T
    ypes and values rather than
    “classes” and “objects”

    View Slide

  11. // data
    http://play.golang.org/p/Mz3rqyIRO7

    View Slide

  12. // behavior (on types)
    http://play.golang.org/p/7wo-feyESu

    View Slide

  13. // behavior (on any type)
    http://play.golang.org/p/lvTRl2fYhh

    View Slide

  14. Composition over Inheritance

    View Slide

  15. Composition (Embedding)
    http://play.golang.org/p/ZZmeDoeMgi

    View Slide

  16. Composition &
    Anonymous field method access
    http://play.golang.org/p/XIepDulSMp

    View Slide

  17. Anonymous field types do not
    enable Sub-Classing
    http://play.golang.org/p/Bw8HcUBfSm

    View Slide

  18. Polymorphism through Interfaces

    View Slide

  19. Interface T
    ype
    http://play.golang.org/p/3BxLmc36q_

    View Slide

  20. In a Nutshell
    Go has types and values rather than classes and objects
    Go provides many of the same features we're used to in classical-
    orientated languages:
    methods on any type
    automatic message delegation through embedding
    polymorphism through interfaces
    Go does not have inheritance, no "is-a" relationships between
    objects
    Go relies on composition for OO

    View Slide

  21. Program in Go

    in the “Go Way”

    View Slide

  22. Go-related
    https://www.youtube.com/watch?v=HqZQBmNVhBw
    http://spf13.com/post/is-go-object-oriented/
    http://nathany.com/good/
    https://www.youtube.com/watch?v=B-r3Wf_I2Lk
    http://www.goinggo.net/2014/05/methods-interfaces-and-embedded-types.html
    http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go
    OOP in general:
    http://www.artima.com/lejava/articles/designprinciples.html
    http://en.wikipedia.org/wiki/Object-oriented_programming
    http://en.wikipedia.org/wiki/Composition_over_inheritance
    http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
    http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
    http://en.wikipedia.org/wiki/Class-based_programming
    Resources

    View Slide

  23. Thanks!

    Q & A

    @jboursiquot

    View Slide