Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Duck! An interface love story

Aaron Cruz
February 19, 2016

Duck! An interface love story

Using interfaces in golang is a lot like duck typing. Find your inner duck with some practical examples.

Aaron Cruz

February 19, 2016
Tweet

More Decks by Aaron Cruz

Other Decks in Technology

Transcript

  1. @mraaroncruz I build prototypes/MVPs to help startups find funding. iOS

    and Android Native Ruby on Anything Go Elixir?
  2. @mraaroncruz I build prototypes/MVPs to help startups find funding. iOS

    and Android Native Ruby on Anything Go Elixir? Indian Food Chef
  3. @mraaroncruz I build prototypes/MVPs to help startups find funding. iOS

    and Android Native Ruby on Anything Go ROSSConf Team Elixir? Indian Food Chef
  4. – Avdi Grimm in Confident Ruby “As confident coders, we

    want to tell our ducks to quack and then move on”
  5. class Pdf def first_page_to_image puts "rip front page" end def

    optimize puts "Optimizing pdf for download" end end
  6. class Pdf def first_page_to_image puts "rip front page" end def

    optimize puts "Optimizing pdf for download" end end class Png def resize puts "creating different sizes for png" end end
  7. class Pdf def first_page_to_image puts "rip front page" end def

    optimize puts "Optimizing pdf for download" end end class Png def resize puts "creating different sizes for png" end end class Txt def zip puts "Zipping text file" end end
  8. # Set up scene pdf = Pdf.new png = Png.new

    text = Txt.new documents = [pdf, png, text]
  9. # Do some work! documents.each do |doc| case doc when

    Pdf doc.first_page_to_image doc.optimize when Png doc.resize when Txt doc.zip end end
  10. # Duck type it! class Pdf def prepare_download first_page_to_image optimize

    end end class Png def prepare_download resize end end class Txt def prepare_download zip end end
  11. # So much simpler documents.each do |doc| doc.prepare_download end #

    or a one liner documents.each(&:prepare_download)
  12. package main import "fmt" type Pdf struct { Body []byte

    } type Png struct { Versions []struct { Version string Body []byte } } type Txt struct { Body string }
  13. func (d *Pdf) RipFirstPage() { fmt.Println("Ripped first page") } func

    (d *Pdf) Optimize() { fmt.Println("Optimize images in pdf") } func (d *Png) Resize() { fmt.Println("Resized png for download") } func (d *Txt) Zip() { fmt.Println("Zipped text file") }
  14. func main() { documents := []interface{}{ &Pdf{}, &Png{}, &Txt{}, }

    for _, doc := range documents { switch doc.(type) { case *Pdf: pdf := doc.(*Pdf) pdf.RipFirstPage() pdf.Optimize() case *Png: png := doc.(*Png) png.Resize() case *Txt: txt := doc.(*Txt) txt.Zip() } } }
  15. func main() { documents := []interface{}{ &Pdf{}, &Png{}, &Txt{}, }

    for _, doc := range documents { switch doc.(type) { case *Pdf: pdf := doc.(*Pdf) pdf.RipFirstPage() pdf.Optimize() case *Png: png := doc.(*Png) png.Resize() case *Txt: txt := doc.(*Txt) txt.Zip() } } }
  16. func main() { documents := []interface{}{ &Pdf{}, &Png{}, &Txt{}, }

    for _, doc := range documents { switch doc.(type) { case *Pdf: pdf := doc.(*Pdf) pdf.RipFirstPage() pdf.Optimize() case *Png: png := doc.(*Png) png.Resize() case *Txt: txt := doc.(*Txt) txt.Zip() } } }
  17. type DownloadPreparer interface { Prepare() } func (d *Pdf) Prepare()

    { d.RipFirstPage() d.Optimize() } func (d *Png) Prepare() { d.Resize() } func (d *Txt) Prepare() { d.Zip() }
  18. type DownloadPreparer interface { Prepare() } func (d *Pdf) Prepare()

    { d.RipFirstPage() d.Optimize() } func (d *Png) Prepare() { d.Resize() } func (d *Txt) Prepare() { d.Zip() }
  19. type DownloadPreparer interface { Prepare() } func (d *Pdf) Prepare()

    { d.RipFirstPage() d.Optimize() } func (d *Png) Prepare() { d.Resize() } func (d *Txt) Prepare() { d.Zip() }
  20. func main() { documents := []DownloadPreparer{ &Pdf{}, &Png{}, &Txt{}, }

    for _, doc := range documents { doc.Prepare() } }