Skip to content

Instantly share code, notes, and snippets.

@dpetersen
Last active August 29, 2015 14:11
Show Gist options
  • Save dpetersen/f25f4a264ae5c14af2d6 to your computer and use it in GitHub Desktop.
Save dpetersen/f25f4a264ae5c14af2d6 to your computer and use it in GitHub Desktop.
type switch
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