gopl-zh.github.com/ch7/ch7-01.md

95 lines
5.8 KiB
Markdown
Raw Normal View History

2016-02-15 03:06:34 +00:00
## 7.1. 接口约定
2015-12-09 07:45:11 +00:00
2016-08-10 15:16:44 +00:00
目前为止,我们看到的类型都是具体的类型。一个具体的类型可以准确的描述它所代表的值,并且展示出对类型本身的一些操作方式:就像数字类型的算术操作,切片类型的取下标、添加元素和范围获取操作。具体的类型还可以通过它的内置方法提供额外的行为操作。总的来说,当你拿到一个具体的类型时你就知道它的本身是什么和你可以用它来做什么。
2015-12-09 07:45:11 +00:00
在Go语言中还存在着另外一种类型接口类型。接口类型是一种抽象的类型。它不会暴露出它所代表的对象的内部值的结构和这个对象支持的基础操作的集合它们只会表现出它们自己的方法。也就是说当你有看到一个接口类型的值时你不知道它是什么唯一知道的就是可以通过它的方法来做什么。
2015-12-09 07:45:11 +00:00
在本书中我们一直使用两个相似的函数来进行字符串的格式化fmt.Printf它会把结果写到标准输出和fmt.Sprintf它会把结果以字符串的形式返回。得益于使用接口我们不必可悲的因为返回结果在使用方式上的一些浅显不同就必需把格式化这个最困难的过程复制一份。实际上这两个函数都使用了另一个函数fmt.Fprintf来进行封装。fmt.Fprintf这个函数对它的计算结果会被怎么使用是完全不知道的。
2016-01-02 13:17:21 +00:00
2015-12-09 07:45:11 +00:00
``` go
package fmt
2016-01-21 02:22:10 +00:00
2015-12-09 07:45:11 +00:00
func Fprintf(w io.Writer, format string, args ...interface{}) (int, error)
func Printf(format string, args ...interface{}) (int, error) {
2015-12-21 09:25:03 +00:00
return Fprintf(os.Stdout, format, args...)
2015-12-09 07:45:11 +00:00
}
func Sprintf(format string, args ...interface{}) string {
2015-12-21 09:25:03 +00:00
var buf bytes.Buffer
Fprintf(&buf, format, args...)
return buf.String()
2015-12-09 07:45:11 +00:00
}
```
2016-01-02 13:17:21 +00:00
2023-07-22 07:11:55 +00:00
Fprintf的前缀F表示文件File也表明格式化输出结果应该被写入第一个参数提供的文件中。在Printf函数中的第一个参数os.Stdout是`*os.File`类型在Sprintf函数中的第一个参数&buf是一个指向可以写入字节的内存缓冲区。然而它并不是一个文件类型尽管它在某种意义上和文件类型相似。
2015-12-09 07:45:11 +00:00
2023-07-22 07:11:55 +00:00
即使是Fprintf函数中的第一个参数它也不是一个文件类型。它是io.Writer类型这是一个接口类型定义如下
2016-01-02 13:17:21 +00:00
2015-12-09 07:45:11 +00:00
``` go
package io
2016-01-21 02:22:10 +00:00
2015-12-21 09:25:03 +00:00
// Writer is the interface that wraps the basic Write method.
type Writer interface {
// Write writes len(p) bytes from p to the underlying data stream.
// It returns the number of bytes written from p (0 <= n <= len(p))
// and any error encountered that caused the write to stop early.
// Write must return a non-nil error if it returns n < len(p).
// Write must not modify the slice data, even temporarily.
//
// Implementations must not retain p.
Write(p []byte) (n int, err error)
2015-12-09 07:45:11 +00:00
}
```
2016-10-18 05:20:38 +00:00
io.Writer类型定义了函数Fprintf和这个函数调用者之间的约定。一方面这个约定需要调用者提供具体类型的值就像`*os.File`和`*bytes.Buffer`这些类型都有一个特定签名和行为的Write的函数。另一方面这个约定保证了Fprintf接受任何满足io.Writer接口的值都可以工作。Fprintf函数可能没有假定写入的是一个文件或是一段内存而是写入一个可以调用Write函数的值。
2015-12-09 07:45:11 +00:00
2018-06-09 16:27:25 +00:00
因为fmt.Fprintf函数没有对具体操作的值做任何假设而是仅仅通过io.Writer接口的约定来保证行为所以第一个参数可以安全地传入一个只需要满足io.Writer接口的任意具体类型的值。一个类型可以自由地被另一个满足相同接口的类型替换被称作可替换性LSP里氏替换。这是一个面向对象的特征。
2015-12-09 07:45:11 +00:00
2018-06-09 16:27:25 +00:00
让我们通过一个新的类型来进行校验,下面`*ByteCounter`类型里的Write方法仅仅在丢弃写向它的字节前统计它们的长度。在这个+=赋值语句中让len(p)的类型和`*c`的类型匹配的转换是必须的。)
2016-01-02 13:17:21 +00:00
2016-01-21 02:22:10 +00:00
<u><i>gopl.io/ch7/bytecounter</i></u>
2015-12-09 07:45:11 +00:00
```go
type ByteCounter int
2016-01-21 02:22:10 +00:00
2015-12-09 07:45:11 +00:00
func (c *ByteCounter) Write(p []byte) (int, error) {
2016-01-21 02:22:10 +00:00
*c += ByteCounter(len(p)) // convert int to ByteCounter
return len(p), nil
2015-12-09 07:45:11 +00:00
}
```
2016-01-21 02:22:10 +00:00
2016-02-15 03:06:34 +00:00
因为*ByteCounter满足io.Writer的约定我们可以把它传入Fprintf函数中Fprintf函数执行字符串格式化的过程不会去关注ByteCounter正确的累加结果的长度。
2016-01-02 13:17:21 +00:00
2015-12-09 07:45:11 +00:00
```go
var c ByteCounter
c.Write([]byte("hello"))
fmt.Println(c) // "5", = len("hello")
2016-01-21 02:22:10 +00:00
c = 0 // reset the counter
2015-12-09 07:45:11 +00:00
var name = "Dolly"
fmt.Fprintf(&c, "hello, %s", name)
fmt.Println(c) // "12", = len("hello, Dolly")
```
2016-01-02 13:17:21 +00:00
2016-02-15 03:06:34 +00:00
除了io.Writer这个接口类型还有另一个对fmt包很重要的接口类型。Fprintf和Fprintln函数向类型提供了一种控制它们值输出的途径。在2.5节中我们为Celsius类型提供了一个String方法以便于可以打印成这样"100°C" 在6.5节中我们给*IntSet添加一个String方法这样集合可以用传统的符号来进行表示就像"{1 2 3}"。给一个类型定义String方法可以让它满足最广泛使用之一的接口类型fmt.Stringer
2016-01-02 13:17:21 +00:00
2015-12-09 07:45:11 +00:00
```go
package fmt
2016-01-21 02:22:10 +00:00
2015-12-09 07:45:11 +00:00
// The String method is used to print values passed
// as an operand to any format that accepts a string
// or to an unformatted printer such as Print.
type Stringer interface {
2016-01-21 02:22:10 +00:00
String() string
2015-12-09 07:45:11 +00:00
}
```
2016-01-02 13:17:21 +00:00
2016-02-15 03:06:34 +00:00
我们会在7.10节解释fmt包怎么发现哪些值是满足这个接口类型的。
2015-12-09 07:45:11 +00:00
**练习 7.1** 使用来自ByteCounter的思路实现一个针对单词和行数的计数器。你会发现bufio.ScanWords非常的有用。
2015-12-09 07:45:11 +00:00
**练习 7.2** 写一个带有如下函数签名的函数CountingWriter传入一个io.Writer接口类型返回一个把原来的Writer封装在里面的新的Writer类型和一个表示新的写入字节数的int64类型指针。
2016-01-02 13:17:21 +00:00
2015-12-09 07:45:11 +00:00
```go
func CountingWriter(w io.Writer) (io.Writer, *int64)
```
2016-01-02 13:17:21 +00:00
2018-06-09 16:27:25 +00:00
**练习 7.3** 为在gopl.io/ch4/treesort§4.4)中的*tree类型实现一个String方法去展示tree类型的值序列。