处理go中的多个错误 [英] Handling multiple errors in go

查看:22
本文介绍了处理go中的多个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,发现错误处理非常冗长.我已经阅读了它的推理并且大部分都同意,但是有一些地方似乎有更多的代码来处理错误而不是实际执行的工作.这是一个(人为的)示例,我在其中输入Hello world!"进入 cat 并读取并打印输出.基本上每一行都有另外三个来处理错误,我什至没有真正处理任何事情.

包主导入fmt"导入io"导入io/ioutil"导入操作系统/执行"功能主(){cmd := exec.Command("cat", "-")标准输入,错误:= cmd.StdinPipe()如果错误!= nil {返回}标准输出,错误:= cmd.StdoutPipe()如果错误!= nil {返回}错误 = cmd.Start()如果错误!= nil {返回}_, err = io.WriteString(stdin, "Hello world!")如果错误!= nil {返回}错误 = stdin.Close();如果错误!= nil {返回}输出,错误:= ioutil.ReadAll(stdout)如果错误!= nil {返回}fmt.Println(字符串(输出))返回}

有没有一种惯用的、干净的方法来处理这个问题?我只是觉得我错过了一些东西.

解决方案

显然,我们必须处理任何错误;我们不能忽视它们.

例如,尝试使示例不那么做作,

包主进口 (fmt"我"io/ioutil"操作系统"操作系统/执行")func 管道(输入字符串)(字符串,错误){cmd := exec.Command("cat", "-")标准输入,错误:= cmd.StdinPipe()如果错误!= nil {返回",错误}标准输出,错误:= cmd.StdoutPipe()如果错误!= nil {返回",错误}错误 = cmd.Start()如果错误!= nil {返回",错误}_, err = io.WriteString(stdin, input)如果错误!= nil {返回",错误}错误 = stdin.Close()如果错误!= nil {返回",错误}全部,错误:= ioutil.ReadAll(stdout)输出:=字符串(全部)如果错误!= nil {返回输出,错误}返回输出,无}功能主(){在 := "你好世界!"fmt.Println(in)出,错误:=管道(入)如果错误!= nil {fmt.Println(err)操作系统退出(1)}fmt.Println(out)}

输出:

世界,你好!你好世界!

<块引用>

错误处理和开始

在 Go 中,错误处理很重要.语言的设计和约定鼓励您明确检查错误所在的位置发生(与其他语言的投掷习惯不同)异常,有时会捕获它们).在某些情况下,这会使 Go代码冗长.

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.

解决方案

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.

这篇关于处理go中的多个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆