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

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

问题描述

我很高兴找到错误处理是非常冗长的。我已经阅读了它的推理,大多同意,但是有几个地方似乎有更多的代码来处理错误,而不是实际做的。这是一个(设计的)例子,我在那里管道Hello world!进入猫并读取并打印输出。基本上每一行都有三个处理错误,而我甚至没有处理任何东西。

  package main 

importfmt
importio
importio / ioutil
importos / exec


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

有没有一个惯用的,干净的方式来处理这个?我只是觉得我错过了一些东西。

解决方案

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



例如,试图让这个例子更加人为,

  package main 

import(
fmt
io
io / ioutil
os
os / exec


func管道(输入字符串)(字符串,错误){
cmd:= exec.Command(cat - )
stdin,err:= cmd.StdinPipe()
如果err!= nil {
return,err
}
stdout,err:= cmd.StdoutPipe()
如果err!= nil {
return,err
}
err = cmd.Start()
如果err!= nil {
return,err
}
_,err = io.WriteString(stdin,input)
如果err!= nil {
return,err
}
err = stdin.Close()
如果err!= nil {
return,err
}
all,err:= ioutil。 ReadAll(stdout)
输出:= string(全部)
如果err!= nil {
返回输出,错误
}
返回输出,nil
}

func main(){
in:=Hello world!
fmt.Println(in)
out,err:=管道(in)
如果err!= nil {
fmt.Println(err)
os.Exit (1)
}
fmt.Println(out)
}

输出:

  Hello world! 
你好世界!




错误处理和访问



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

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

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