| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
type S struct{ |
| 10 |
m1Called, m2Called bool |
| 11 |
} |
| 12 |
|
| 13 |
func (s *S) M1(int) (int, int) { |
| 14 |
s.m1Called = true |
| 15 |
return 0, 0 |
| 16 |
} |
| 17 |
|
| 18 |
func (s *S) M2(int) (int, int) { |
| 19 |
s.m2Called = true |
| 20 |
return 0, 0 |
| 21 |
} |
| 22 |
|
| 23 |
type C struct { |
| 24 |
calls []func(int) (int, int) |
| 25 |
} |
| 26 |
|
| 27 |
func makeC() Funcs { |
| 28 |
return &C{} |
| 29 |
} |
| 30 |
|
| 31 |
func (c *C) Add(fn func(int) (int, int)) Funcs { |
| 32 |
c.calls = append(c.calls, fn) |
| 33 |
return c |
| 34 |
} |
| 35 |
|
| 36 |
func (c *C) Call() { |
| 37 |
for _, fn := range c.calls { |
| 38 |
fn(0) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
type Funcs interface { |
| 43 |
Add(func(int) (int, int)) Funcs |
| 44 |
Call() |
| 45 |
} |
| 46 |
|
| 47 |
func main() { |
| 48 |
s := &S{} |
| 49 |
c := makeC().Add(s.M1).Add(s.M2) |
| 50 |
c.Call() |
| 51 |
if !s.m1Called || !s.m2Called { |
| 52 |
panic("missed method call") |
| 53 |
} |
| 54 |
} |