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:
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
SO Question: Go — handling multiple errors elegantly?
SO Question: More terse error handling in Go
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.