package main import ( "bufio" "fmt" "io" "os" "strings" ) type ByteCount int func (bc *ByteCount) Write(p []byte) (n int,err error) { *bc = ByteCount(len(p)) return len(p),nil } type WordsNum int func (wn *WordsNum) Write(p []byte) (n int,err error) { *wn = WordsNum(len(p)) return len(p),nil } func Count() []byte { scanner := bufio.NewScanner(os.Stdin) fmt.Print("Enter text here: ") if scanner.Scan() { line := scanner.Text() wordCount := bufio.NewScanner(strings.NewReader(line)) wordCount.Split(bufio.ScanWords) wordsNum := []byte{} for wordCount.Scan() { wordsNum = append(wordsNum, 1) fmt.Println("Word is: ",wordCount.Text()) } fmt.Printf("Amount of words is %d\n",wordsNum) return wordsNum } return nil } type NewWriter struct { writer io.Writer counter *int64 } func (nw *NewWriter) Write(p []byte) (n int,err error) { n,err = nw.writer.Write(p) fmt.Println() *nw.counter = int64(n) return n,err } func CountingWriter(w io.Writer) (io.Writer, *int64) { counter := int64(0) return &NewWriter{ writer: w, counter: &counter, },&counter } // Упражнения на возврат LimitReader func GetNewReader(s string) io.Reader { return strings.NewReader(s) } type MyReader struct { reader io.Reader constraint int } func (nr MyReader) Read(p []byte) (n int,err error) { n,err = nr.reader.Read(p[:nr.constraint]) return } func LimitReader(r io.Reader,n int) io.Reader { return MyReader{ reader: r, constraint: n, } } func main() { // var bc ByteCount // bc.Write([]byte("Hello dodik")) // fmt.Println(bc) // var wn WordsNum // wn.Write(Count()) // fmt.Println(wn) // writer,count := CountingWriter(os.Stdout) // writer.Write([]byte("Hello Baga")) // fmt.Printf("Bytes %d",*count) str := "hello" buf := make([]byte,100) reader := LimitReader(strings.NewReader(str),3) // n,_ := reader.Read(buf) // reader := strings.NewReader(str) n,_ := reader.Read(buf) fmt.Printf("Result is %d\n",n) fmt.Printf("String is %s",string(buf)) }