dongque1646 2016-01-23 03:54
浏览 47
已采纳

在Go中,当一个函数返回错误时,其他变量是否总是其“零”值?

I'm talking about the Go standard library:

output, err := abc.Xyz()
if err != nil {
    // by convention is `output` always its "zero" value?
}
  • 写回答

1条回答 默认 最新

  • doumei1772 2016-01-23 03:59
    关注

    Not always. For example,io.Reader:

    Package io

    type Reader

    type Reader interface {
            Read(p []byte) (n int, err error)
    }
    

    Reader is the interface that wraps the basic Read method.

    Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.

    When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.

    Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.

    Implementations of Read are discouraged from returning a zero byte count with a nil error, except when len(p) == 0. Callers should treat a return of 0 and nil as indicating that nothing happened; in particular it does not indicate EOF.

    Implementations must not retain p.

    For example,

    readfile.go:

    package main
    
    import (
        "bufio"
        "fmt"
        "io"
        "os"
    )
    
    func main() {
        f, err := os.Open("readfile.go")
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f.Close()
        r := bufio.NewReader(f)
    
        fileLen := int64(0)
        buf := make([]byte, 0, 4*1024)
        for {
            n, err := r.Read(buf[:cap(buf)])
            buf = buf[:n]
            if n == 0 {
                if err == nil {
                    continue
                }
                if err == io.EOF {
                    break
                }
                fmt.Println(err)
                return
            }
    
            // Do something with buf
            fileLen += int64(len(buf))
    
            if err != nil && err != io.EOF {
                fmt.Println(err)
                return
            }
        }
        fmt.Println("file length:", fileLen, "bytes")
    }
    

    If err != nil then, unless the documentation says otherwise, assume that all other values are undefined.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA