Skip to content

Instantly share code, notes, and snippets.

Created November 9, 2016 16:17
Show Gist options
  • Select an option

  • Save anonymous/e1f04d24747f04111c72007a7cc1bc9f to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/e1f04d24747f04111c72007a7cc1bc9f to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Nov 9, 2016.
    13 changes: 13 additions & 0 deletions test_a_sayer.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    package a

    import "fmt"

    type Sayer interface {
    Say() string
    }

    type Formal struct{}

    func (p Formal) Greet(s interface{Sayer}) string {
    return fmt.Sprintf("Hello, %s", s.Say())
    }
    13 changes: 13 additions & 0 deletions test_b_sayer.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    package b

    import "fmt"

    type Sayer interface {
    Say() string
    }

    type Casual struct{}

    func (p Casual) Greet(s interface{Sayer}) string {
    return fmt.Sprintf("Hey %s!", s.Say())
    }
    13 changes: 13 additions & 0 deletions test_greet_greet.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    package greet

    type Sayer interface {
    Say() string
    }

    type Greeter interface {
    Greet(s interface{Sayer}) string
    }

    func Greet(g interface{Greeter}, i Sayer) string {
    return g.Greet(i)
    }
    31 changes: 31 additions & 0 deletions test_main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    package main

    import (
    "log"
    "test/a"
    "test/b"
    "test/greet"
    )

    type Who struct {
    to string
    }

    func (w Who) Say() string {
    return w.to
    }

    func main() {
    log.Println("Testing...")

    a := a.Formal{}
    b := b.Casual{}


    w := &Who{
    to:"world",
    }

    log.Println(greet.Greet(a, w))
    log.Println(greet.Greet(b, w))
    }