| 1 | package main |
| 2 | |
| 3 | type I interface { |
| 4 | Foo() int |
| 5 | } |
| 6 | |
| 7 | type S1 struct { |
| 8 | i int |
| 9 | } |
| 10 | |
| 11 | func (s S1) Foo() int { return s.i } |
| 12 | |
| 13 | type S2 struct{} |
| 14 | |
| 15 | func (s *S2) Foo() int { return 42 } |
| 16 | |
| 17 | func main() { |
| 18 | Is := map[string]I{ |
| 19 | "foo": S1{21}, |
| 20 | "bar": &S2{}, |
| 21 | } |
| 22 | n := 0 |
| 23 | for _, s := range Is { |
| 24 | n += s.Foo() |
| 25 | } |
| 26 | bar := "bar" |
| 27 | println(n, Is["foo"].Foo(), Is[bar].Foo()) |
| 28 | } |
| 29 | |
| 30 | // Output: |
| 31 | // 63 21 42 |