Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created July 11, 2014 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodokaiser/6e0e424e66ce13364cde to your computer and use it in GitHub Desktop.
Save bodokaiser/6e0e424e66ce13364cde to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
type Result interface {
Value() string
}
type SpecialResult struct {
value string
}
func (r *SpecialResult) Value() string {
return r.value
}
func (r *SpecialResult) Special() string {
return "look I am special!!"
}
func main() {
sr := &SpecialResult{
value: "1234",
}
doSomething(sr)
}
func doSomething(r Result) {
switch reflect.TypeOf(r).String() {
case "*main.SpecialResult":
fmt.Printf("%s, %s\n", r.Value(), r.(*SpecialResult).Special())
break
default:
fmt.Printf("%s\n", r.Value())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment