我如何进一步理解Go为何如此处理错误? [英] How can I further my understanding of why Go handles errors the way it does?

查看:156
本文介绍了我如何进一步理解Go为何如此处理错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用GoogledGolang错误,执行错误处理,执行错误和执行错误冗长来了解其他人对此主题的看法,以及在线阅读一些教程。



我也观看了这些YouTube视频: https:/ /www.youtube.com/watch?v=Ph4eYD7Bgek



但我仍然不明白为什么我必须在我做的所有事情之后添加这些行:

  if err!= nil {
fmt.Println(err)
}

为什么这不会自动发生?我仍然不明白为什么这种方式比尝试更好,赶上。我来自PHP背景,在PHP中,我几乎从不做错误检查,每当出现问题时它会自动记录东西,那么为什么我必须在Go上手动执行此操作?有什么好处?我真的很想100%地学习这门语言,但是一次又一次地写同样的代码感觉非常错误。这与我从编程中学到的东西是不符的,不要重复自己。



我怎样才能更明白这一点?这是什么原因和好处?也许我会理解,如果某人遇到了某些情况的例子,为什么这种处理错误的方式是最好的方式,而不是尝试捕获,这种方式总比不检查更好,就像我在PHP中一样。

b
$ b

我已经阅读了Go的官方网站。以及其他各种在线教程,例如:



https://gobyexample.com /



Oreilly的视频教程。



来自Pluralsight的视频教程。

解决方案

我希望这是一条评论,但链接太多:

博客文章:错误处理和Go



有效转到:错误



SO答案:错误返回



SO问题:去 - 优雅地处理多个错误?



SO Questi on:在Go上更加简洁的错误处理



博客文章:错误是值

代码复习问题:一个最小版本控制系统



函数返回带有上下文的错误处理程序函数:

  func ger(ctxt string)func(string,error)error {
return func(msg string,err error )错误{
return fmt.Errorf(%s:%s:%v,ctxt,msg,err)
}
}

//使用它:
er:= ger(Handling cmd)
er(Add,err)

...名单继续使用Google和SO搜索。

I have Googled "Golang errors", "Go error handling", "Go errors" and "Go error verbose" to see what other people say on the subject, and also to read on some tutorials online.

I have also watched YouTube videos like these: https://www.youtube.com/watch?v=Ph4eYD7Bgek

But I still can not understand why I have to add these lines after everything I do:

if err != nil {
    fmt.Println(err)
}

Why does this not happen automatically? And I still cannot understand why this way is better than try, catch. I come from a PHP background, and in PHP, I almost never did error checking, every time something went wrong it logged stuff automatically, so why do I have to this manually on Go? What are the benefits? I really want to "Go" 100% about learning this language, but it feels so wrong to write the same code over and over again. It goes against what I have learned about programming, to not repeat yourself.

How can I understand this more? What is the reason and benefits of this? Maybe I would understand if someone came with examples of some situations why this way of handling error is the best way, and not try catch, and this way is better than not checking at all, like I did in PHP.

And I have already read on the official website of Go. And various other tutorials online such as:

https://gobyexample.com/

Video tutorial by Oreilly.

Video tutorial from Pluralsight.

解决方案

I wanted this to be a comment, but there are too many links:

Blog post: Error handling and Go

Effective Go: Errors

SO Answer: Error returns

SO Question: Go — handling multiple errors elegantly?

SO Question: More terse error handling in Go

Blog post: Errors are values

Code Review Question: A minimal version control system

Function that returns an "error handler" function with context:

func ger(ctxt string) func(string, error) error {
    return func(msg string, err error) error {
        return fmt.Errorf("%s : %s : %v", ctxt, msg, err)
    }
}

// And using it:
er := ger("Handling cmd")
er("Add", err)

...the list goes on, use Google and SO search.

这篇关于我如何进一步理解Go为何如此处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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