Last active
August 29, 2015 14:11
-
-
Save dpetersen/f25f4a264ae5c14af2d6 to your computer and use it in GitHub Desktop.
type switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Fetchable interface { | |
URL() string | |
} | |
type Completable interface { | |
HasCompleted() | |
} | |
type Foo struct{} | |
type Bar struct{} | |
func (f Foo) URL() string { | |
return "example.cm/foo" | |
} | |
func (b Bar) URL() string { | |
return "example.com/bar" | |
} | |
func (s Bar) HasCompleted() { | |
fmt.Println("Bar has completed") | |
} | |
func PresentURL(f Fetchable) { | |
fmt.Println("URL: ", f.URL()) | |
switch o := f.(type) { | |
case Completable: | |
o.HasCompleted() | |
default: | |
fmt.Println("This item is not completable") | |
} | |
} | |
func main() { | |
f, b := Foo{}, Bar{} | |
PresentURL(f) | |
PresentURL(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment