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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图