You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
489 B
42 lines
489 B
12 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"reflect"
|
||
|
)
|
||
|
|
||
|
type Fooer interface {
|
||
|
Foo(int) int
|
||
|
}
|
||
|
|
||
|
type Bar struct {
|
||
|
SomeField int
|
||
|
}
|
||
|
|
||
|
func (this *Bar) Foo(x int) int {
|
||
|
return x
|
||
|
}
|
||
|
|
||
|
type Baz struct {
|
||
|
SomeField int
|
||
|
}
|
||
|
|
||
|
func (this Baz) Foo(x int) int {
|
||
|
return x
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
var bar, baz Fooer
|
||
|
|
||
|
bar = &Bar{
|
||
|
SomeField: 5,
|
||
|
}
|
||
|
|
||
|
baz = Baz{
|
||
|
SomeField: 5,
|
||
|
}
|
||
|
|
||
|
fmt.Println(reflect.ValueOf(baz).FieldByName("SomeField"))
|
||
|
fmt.Println(reflect.Indirect(reflect.ValueOf(bar)).FieldByName("SomeField"))
|
||
|
}
|