understanding the interface

In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other
展开查看详情

1.understanding the interface @francesc

2.what is an interface?

3."In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other" - wikipedia

4."In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other" - wikipedia

5."In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other" - wikipedia

6.

7.

8.

9.what is a Go interface?

10.abstract types concrete types

11.concrete types in Go - they describe a memory layout int8 int16 int32 int64 - behavior attached to data through methods

12. *gzip.Writer *strings.Reader []bool int *os.File

13.abstract types in Go - they describe behavior io.Reader io.Writer fmt.Stringer - they define a set of methods, without specifying the receiver

14.two interfaces

15. io.Writer *gzip.Writer *strings.Reader []bool io.Reader int *os.File

16.union of interfaces

17.union of interfaces

18. io.Writer *gzip.Writer *strings.Reader []bool io.Reader int *os.File io.ReadWriter

19. ? io.Writer *gzip.Writer *strings.Reader []bool io.Reader int *os.File io.ReadWriter

20.interface{}

21.“interface{} says nothing” - Rob Pike in his Go Proverbs

22.

23.why do we use interfaces?

24.why do we use interfaces? - writing generic algorithms - hiding implementation details - providing interception points

25.what function do you prefer?

26.Cons: how would you test it? what if you want to write to memory? Pros: ?

27.Cons: how do you even write to interface{}? probably requires runtime checks Pros: you can write really bad code

28.Which ones does WriteTo really need? - Write - Read - Close

29.“The bigger the interface, the weaker the abstraction” - Rob Pike in his Go Proverbs