go-proverbs / go-proverbs.github.io

Inspired by @rob_pike talk at Gopherfest SV 2015
MIT License
334 stars 21 forks source link

Proposal: Readers Read(...), Writers Write(...) #25

Open nyk opened 7 years ago

nyk commented 7 years ago

A famous Chinese proverb attributed to Kong Fu Tse is, "Fathers father, sons son", meaning that the name of "father" is given to a man who raises children (fathers them), and the name of "son" is given to a boy/man who acts with filial duty (sons: as a verb). A name or role is properly granted only to those duties or functions it can perform; functions that are expected from the one bearing the name.

This is how we treat the implementation of Go interfaces implicitly, in that if a type has a function with the "Read(p []byte) (n int, err error)" specification it means it is capable of acting like a Reader, and therefore it IS a Reader. Likewise, we call a Writer that which can write (implements the Write(...) function).