How to implement try - catch mechanism in go?

Examples will be appreciated.Thank you!!!

Do not. This is not the way errors are handled in Go.

if you could give an example on the right way to handle errors in go

Sure, here’s an article on it:

https://blog.golang.org/error-handling-and-go

The Go tour also has a section on it, which might be more easily digested:

https://tour.golang.org/methods/19

well go does not support try/catch or exception if you may. simple if/else are more then sufficient most of the time.

go does not support try catch but it is able to return multiple result, most often the library you are using will return error object when there is error, so detecting the variable != nil is enough to know whether the function has an exception

because of this, when you are designing your software, you need to always carefully check on your data and create a new error object when there is error and dont left it untreated, returning error simply errors.New(“the message of error”)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.