第十二章修改 (#838)

Co-authored-by: Joe Chen <jc@unknwon.io>
This commit is contained in:
Haigang Zhou
2022-05-12 21:59:20 +08:00
committed by GitHub
parent e394361869
commit d29644465a
13 changed files with 83 additions and 83 deletions

View File

@@ -1,6 +1,6 @@
# 12.8 使用接口的实际例子fmt.Fprintf
例子程序 `io_interfaces.go` 很好的阐述了 io 包中的接口概念。
例子程序 `io_interfaces.go` 很好的阐述了 `io` 包中的接口概念。
示例 12.15 [io_interfaces.go](examples/chapter_12/io_interfaces.go)
@@ -38,7 +38,7 @@ hello world! - buffered
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
```
不是写入一个文件,而是写入一个 `io.Writer` 接口类型的变量,下面是 `Writer` 接口在 io 包中的定义:
不是写入一个文件,而是写入一个 `io.Writer` 接口类型的变量,下面是 `Writer` 接口在 `io` 包中的定义:
```go
type Writer interface {
@@ -46,9 +46,9 @@ type Writer interface {
}
```
`fmt.Fprintf()` 依据指定的格式向第一个参数内写入字符串,第一个参数必须实现了 `io.Writer` 接口。`Fprintf()` 能够写入任何类型,只要其实现了 `Write` 方法,包括 `os.Stdout`,文件(例如 os.File管道网络连接通道等等同样也可以使用 bufio 包中缓冲写入。bufio 包中定义了 `type Writer struct{...}`
`fmt.Fprintf()` 依据指定的格式向第一个参数内写入字符串,第一个参数必须实现了 `io.Writer` 接口。`Fprintf()` 能够写入任何类型,只要其实现了 `Write` 方法,包括 `os.Stdout`,文件(例如 `os.File`),管道,网络连接,通道等等同样地,也可以使用 `bufio` 包中缓冲写入。`bufio` 包中定义了 `type Writer struct{...}`
bufio.Writer 实现了 Write 方法:
`bufio.Writer` 实现了 `Write()` 方法:
```go
func (b *Writer) Write(p []byte) (nn int, err error)
@@ -64,7 +64,7 @@ func NewWriter(wr io.Writer) (b *Writer)
在缓冲写入的最后千万不要忘了使用 `Flush()`,否则最后的输出不会被写入。
在 15.2-15.8 章节,我们将使用 `fmt.Fprint` 函数向 `http.ResponseWriter` 写入,其同样实现了 io.Writer 接口。
[15.2](15.2.md)-[15.8](15.8.md) 章节,我们将使用 `fmt.Fprint()` 函数向 `http.ResponseWriter` 写入,其同样实现了 `io.Writer` 接口。
**练习 12.7**[remove_3till5char.go](exercises/chapter_12/remove_3till5char.go)