interfaces.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. package interfaces
  5. type I interface {
  6. Rand() int32
  7. }
  8. type SameI interface {
  9. Rand() int32
  10. }
  11. type LargerI interface {
  12. Rand() int32
  13. AnotherFunc()
  14. }
  15. func Add3(r I) int32 {
  16. return r.Rand() + r.Rand() + r.Rand()
  17. }
  18. // chosen by fair dice roll.
  19. // guaranteed to be random.
  20. type seven struct{}
  21. func (seven) Rand() int32 { return 7 }
  22. func Seven() I { return seven{} }
  23. type WithParam interface {
  24. HasParam(p bool)
  25. }
  26. type Error interface {
  27. Err() error
  28. }
  29. func CallErr(e Error) error {
  30. return e.Err()
  31. }
  32. // not implementable
  33. type I1 interface {
  34. J()
  35. H() *seven // not bound
  36. }
  37. // not implementable
  38. type I2 interface {
  39. f()
  40. G()
  41. }
  42. // implementable
  43. // (the implementor has to find a source of I1s)
  44. type I3 interface {
  45. F() I1
  46. }
  47. // not bound
  48. func F() seven { return seven{} }
  49. func G(u seven) {}
  50. // Interfaces is an interface with the same name as its package.
  51. type Interfaces interface {
  52. SomeMethod()
  53. }