dongshi4589
2015-10-29 03:16
浏览 153
已采纳

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

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. source code of io and flate.
io: https://golang.org/src/io/io.go
flate: https://golang.org/src/compress/flate/inflate.go

图片转代码服务由CSDN问答提供 功能建议

我在golang的zlib中发现了许多代码段,例如 r。(flate.Reader) /reader.go文件。 是什么意思?
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 {
 zr = fr 
} else {
 zr = bufio.NewReader(r)
} 
 //省略更多代码... 
} 
   
  
 

PS io flate 的源代码。
io: https://golang.org/src/io/io.go
flate: https://golang.org/src/compress/flate/inflate.go

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douxi3977 2015-10-29 04:09
    最佳回答

    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) is a type assertion. For example,

    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 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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题