3 ms·
Any error return values in go must be assigned to a variable at call time, otherwise it's a compile error. Unused variables are also a compile error, so you are
by Conlectus 10y ago
Any error return values in go must be assigned to a variable at call time, otherwise it's a compile error. Unused variables are also a compile error, so you are forced to do something with it.
Assigning to _ effectively ignores the error, but is considered a very bad practice.
Unavoidable runtime errors (eg divide by 0) will generate a panic, which is similar to an exception, but is uncatchable within the Go routine it originates in.
- danieldk 10y agoAny error return values in go must be assigned to a variable at call time, otherwise it's a compile error. Nonsense. fmt.Println("Hello world!") I just ignored an error and it compiles fine. Go check the return type of Println: https://golang.org/pkg/fmt/#Println https://golang.org/pkg/fmt/#Println tl;dr, it's very easy to accidentally ignore errors in functions that are side-effecting or modify parameters. Been there, done that.
- pmarreck 10y agoYeah, that's what I thought. And I've also been there, done that. Is there basically some willful blindness around this issue with Go fans, or something? Or a lack of experience doing code? Or am I completely missing something?