Skip to content

Instantly share code, notes, and snippets.

@heiwa4126
Created June 26, 2020 04:48
Show Gist options
  • Save heiwa4126/c9b801e011821da6f864a664e64a8fb6 to your computer and use it in GitHub Desktop.
Save heiwa4126/c9b801e011821da6f864a664e64a8fb6 to your computer and use it in GitHub Desktop.
io.Writeインタフェースを持つ構造体Hogeの例
package main
import (
"fmt"
)
type Hoge struct {
label string
cnt int
}
func (h *Hoge) Write(b []byte) (n int, err error) {
h.cnt++
fmt.Printf("(%s:%d) len=%d bytes\n", h.label, h.cnt, len(b))
return len(b), nil
}
func main() {
h := &Hoge{"hoge1", 0}
fmt.Printf("%#v\n", h)
fmt.Fprintf(h, "Hello, world")
fmt.Fprintf(h, "Hello, world once again")
fmt.Printf("%#v\n", h)
}
@heiwa4126
Copy link
Author

heiwa4126 commented Jun 26, 2020

実行結果 - The Go Playground

&main.Hoge{label:"hoge1", cnt:0}
(hoge1:1) len=12 bytes
(hoge1:2) len=23 bytes
&main.Hoge{label:"hoge1", cnt:2}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment