Exercises io.Writer Interface

Exercises

https://go.dev/play/p/Fdm5p0ZLnrK

package main

import "fmt"

// ByteCounter - counts bytes written
type ByteCounter int

// Implement Write method for ByteCounter
// to count the number of bytes written.
// /usr/local/Cellar/go/1.17.5/libexec/src/io/io.go
// type Writer interface {
//     Write(p []byte) (n int, err error)
// }
func (b *ByteCounter) Write(p []byte) (int, error) {
    *b += ByteCounter(len(p))
    return len(p), nil
}

func main() {
    var b ByteCounter
    fmt.Fprintf(&b, "hello world")
    fmt.Println(b)
}

// output: 11
© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:53

results matching ""

    No results matching ""

    results matching ""

      No results matching ""