dthswrp84966 2013-04-20 23:01
浏览 227
已采纳

处理多个错误

I'm new to go and finding the error handling to be extremely verbose. I've read the reasoning for it and mostly agree, but there are a few places where it seems like there's more code to handle errors than actually do the work. Here is a (contrived) example, where I pipe "Hello world!" into cat and read and print the output. Basically every line has three more to handle the error, and I'm not really even handling anything.

package main

import "fmt"
import "io"
import "io/ioutil"
import "os/exec"


func main() {
    cmd := exec.Command("cat", "-")
    stdin, err := cmd.StdinPipe()
    if err != nil {
        return
    }
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return
    }
    err = cmd.Start()
    if err != nil {
        return
    }
    _, err = io.WriteString(stdin, "Hello world!")
    if err != nil {
        return
    }
    err = stdin.Close();
    if err != nil {
        return
    }
    output, err := ioutil.ReadAll(stdout)
    if err != nil {
        return
    }
    fmt.Println(string(output))
    return
}

Is there an idiomatic, clean way to handle this? I just feel like I'm missing something.

  • 写回答

4条回答 默认 最新

  • duandianzhong8315 2013-04-20 23:56
    关注

    Clearly, we must handle any errors; we can't just ignore them.

    For example, trying to make the example less artificial,

    package main
    
    import (
        "fmt"
        "io"
        "io/ioutil"
        "os"
        "os/exec"
    )
    
    func piping(input string) (string, error) {
        cmd := exec.Command("cat", "-")
        stdin, err := cmd.StdinPipe()
        if err != nil {
            return "", err
        }
        stdout, err := cmd.StdoutPipe()
        if err != nil {
            return "", err
        }
        err = cmd.Start()
        if err != nil {
            return "", err
        }
        _, err = io.WriteString(stdin, input)
        if err != nil {
            return "", err
        }
        err = stdin.Close()
        if err != nil {
            return "", err
        }
        all, err := ioutil.ReadAll(stdout)
        output := string(all)
        if err != nil {
            return output, err
        }
        return output, nil
    }
    
    func main() {
        in := "Hello world!"
        fmt.Println(in)
        out, err := piping(in)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        fmt.Println(out)
    }
    

    Output:

    Hello world!
    Hello world!
    

    Error Handling and Go

    In Go, error handling is important. The language's design and conventions encourage you to explicitly check for errors where they occur (as distinct from the convention in other languages of throwing exceptions and sometimes catching them). In some cases this makes Go code verbose.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式