| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package interfaces |
| 6 |
|
| 7 |
type I interface { |
| 8 |
Rand() int32 |
| 9 |
} |
| 10 |
|
| 11 |
type SameI interface { |
| 12 |
Rand() int32 |
| 13 |
} |
| 14 |
|
| 15 |
type LargerI interface { |
| 16 |
Rand() int32 |
| 17 |
AnotherFunc() |
| 18 |
} |
| 19 |
|
| 20 |
func Add3(r I) int32 { |
| 21 |
return r.Rand() + r.Rand() + r.Rand() |
| 22 |
} |
| 23 |
|
| 24 |
// chosen by fair dice roll. |
| 25 |
// guaranteed to be random. |
| 26 |
type seven struct{} |
| 27 |
|
| 28 |
func (seven) Rand() int32 { return 7 } |
| 29 |
|
| 30 |
func Seven() I { return seven{} } |
| 31 |
|
| 32 |
type WithParam interface { |
| 33 |
HasParam(p bool) |
| 34 |
} |
| 35 |
|
| 36 |
type Error interface { |
| 37 |
Err() error |
| 38 |
} |
| 39 |
|
| 40 |
func CallErr(e Error) error { |
| 41 |
return e.Err() |
| 42 |
} |
| 43 |
|
| 44 |
// not implementable |
| 45 |
type I1 interface { |
| 46 |
J() |
| 47 |
H() *seven // not bound |
| 48 |
} |
| 49 |
|
| 50 |
// not implementable |
| 51 |
type I2 interface { |
| 52 |
f() |
| 53 |
G() |
| 54 |
} |
| 55 |
|
| 56 |
// implementable |
| 57 |
// (the implementor has to find a source of I1s) |
| 58 |
type I3 interface { |
| 59 |
F() I1 |
| 60 |
} |
| 61 |
|
| 62 |
// not bound |
| 63 |
func F() seven { return seven{} } |
| 64 |
func G(u seven) {} |
| 65 |
|
| 66 |
// Interfaces is an interface with the same name as its package. |
| 67 |
type Interfaces interface { |
| 68 |
SomeMethod() |
| 69 |
} |