Go Language Bible-interface
1. Interface types are abstractions and generalizations of other types of behavior
The uniqueness of the interface type in 2.Go language is that it satisfies the implicit implementation
There is another type in the 3.Go language: the interface type. An interface type is an abstract type
4. One type is free to replace with another type that satisfies the same interface, called replaceable (LSP Richter replacement)
Exercise 7.1: Use the idea from bytecounter to implement a counter for the number of words and rows. You'll find Bufio. Scanwords is very useful.
Package Mainimport ( "Bufio" " fmt" " Strings") func main () { var c wordscounter FMT. fprintf (&c, "Hello World 123") FMT. PRINTLN (c)//output 3}/* Exercise 7.1: Use the idea from bytecounter to implement a counter for the number of words and rows. You'll find Bufio. Scanwords is very useful. */type Bytecounter int func (c *bytecounter) Write (P []byte) (int, error) { *c + = Bytecounter (len (P))//convert int t o bytecounter return len (P), nil}//definition type wordscounter int//satisfies the same interface type func (w *wordscounter) Write (P []byte) (int, Error) { //delimited string s: = strings. Newreader (String (p)) bs: = Bufio. Newscanner (s) BS. Split (Bufio. scanwords) sum: = 0 for bs. Scan () { sum++ } *w = Wordscounter (sum) return sum, nil}
Daily Go Language Bible--Interface conventions exercises