gpt4 book ai didi

go - “r.(flate.Reader)”在golang的zlib/reader.go文件中是什么意思?

转载 作者:IT王子 更新时间:2023-10-29 01:33:26 28 4
gpt4 key购买 nike

我在golang的zlib/reader.go文件中找到了很多像r.(flate.Reader)这样的代码片段。这是什么意思?
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 ...
}

附言 ioflate的源代码。
io: https://golang.org/src/io/io.go
Flate: https://golang.org/src/compress/flate/inflate.go

最佳答案

The Go Programming Language Specification

Type assertions

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

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)

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 ...
}
rio.Reader类型 interfacefr, ok := r.(flate.Reader)检查 r以查看其是否包含 io.Reader类型的 flate.Reader

关于go - “r.(flate.Reader)”在golang的zlib/reader.go文件中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405373/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com