"r.(flate.Reader)"是什么意思?在golang的zlib/reader.go文件中是什么意思? [英] What does "r.(flate.Reader)" mean in golang's zlib/reader.go file?

查看:60
本文介绍了"r.(flate.Reader)"是什么意思?在golang的zlib/reader.go文件中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在golang的zlib/reader.go文件中发现了许多代码片段,例如 r.(flate.Reader).什么意思?
https://golang.org/src/compress/zlib/reader.go

I found lots of code snippets like r.(flate.Reader) in golang's zlib/reader.go file. What does it mean?
https://golang.org/src/compress/zlib/reader.go

func (z *reader) Reset(r io.Reader, dict []byte) error {
    if fr, ok := r.(flate.Reader); ok {
        z.r = fr
    } else {
        z.r = bufio.NewReader(r)
    }
    // more code omitted ...
}

P.S. io flate 的源代码.
io: https://golang.org/src/io/io.go
Flate: https://golang.org/src/compress/flate/inflate.go

P.S. source code of io and flate.
io: https://golang.org/src/io/io.go
flate: https://golang.org/src/compress/flate/inflate.go

推荐答案

Go编程语言规范

类型声明

对于接口类型为T且类型为T的表达式x,主要表达

For an expression x of interface type and a type T, the primary expression

x.(T)

断言x不是nil,并且存储在x中的值是T类型.x.(T)称为类型断言.

asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

在类型的赋值或初始化中使用的类型断言特殊形式

A type assertion used in an assignment or initialization of the special form

v, ok = x.(T)
v, ok := x.(T)
var v, ok = x.(T)

产生一个附加的未类型化的布尔值.如果ok的值为true断言成立.否则为假,v的值为类型T的值为零.在这种情况下,不会发生运行时恐慌.C

yields an additional untyped boolean value. The value of ok is true if the assertion holds. Otherwise it is false and the value of v is the zero value for type T. No run-time panic occurs in this case. C

r.(flate.Reader)是类型断言.例如,

func (z *reader) Reset(r io.Reader, dict []byte) error {
    if fr, ok := r.(flate.Reader); ok {
        z.r = fr
    } else {
        z.r = bufio.NewReader(r)
    }
    // more code omitted ...
}

r 的类型为 io.Reader ,是一个接口. fr,好的:= r.(flate.Reader)检查 r 来查看它是否包含类型为 flate的 io.Reader .阅读器.

r is type io.Reader, an interface. fr, ok := r.(flate.Reader) checks r to see if it contains an io.Reader of type flate.Reader.

这篇关于"r.(flate.Reader)"是什么意思?在golang的zlib/reader.go文件中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆