simple_interface.go 416 B

1234567891011121314151617181920212223242526272829303132333435
  1. // simple_interface.go
  2. package main
  3. import (
  4. "fmt"
  5. )
  6. type Simpler interface {
  7. Get() int
  8. Put(int)
  9. }
  10. type Simple struct {
  11. i int
  12. }
  13. func (p *Simple) Get() int {
  14. return p.i
  15. }
  16. func (p *Simple) Put(u int) {
  17. p.i = u
  18. }
  19. func fI(it Simpler) int {
  20. it.Put(5)
  21. return it.Get()
  22. }
  23. func main() {
  24. var s Simple
  25. fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
  26. }
  27. // Output: 5