It’s not that hard to forget that the hello function returns an error (in a more complicated scenario) and if you were just code reviewing main (assume the hello func is in some lib) a reviewer would definitely miss the fact that an error might have occurred.
If I saw a call to a function that took no params and returned nothing, I would probably examine it. I don’t think the Go compiler is trying to prevent you from doing certain things like ignoring errors. It’s just trying to make you do so intentionally. This, to me, seems intentional.
I’ve been bitten by plenty of things while writing software. Can’t say that calling a function named hello while ignoring that it might return something has been one of them. Especially in go, given that it’s idiomatic to return errors if errors are possible. And thus it’s idiomatic to also check for errors. What’s the real-world scenario you are trying to protect against?
I guess my example was quite contrived. I was just trying to illustrate the function signature that would cause the issue I was asking about. The context for the question is that I have been working to bring Go into my team at work - so I’ve been trying to learn a lot about the smaller edge cases in the language and to understand where a new Go programmer might make mistakes.
I would also counter that Go does attempt to ensure you don’t make simple mistakes (maybe not errors specifically since they are just another value). To me, my hello example would be the same thing as attempting to do this (again just a contrived example for illustrative purposes):
Which of course does not compile. Instead, the compiler says: ./prog.go:12:11: assignment mismatch: 1 variable but executeUpdate returns 2 values.
Why doesn’t my hello example fail to compile with the same type of error (e.g. assignment mismatch: 0 variables but hello returns 1 value)?
I would expect that you would need to write _ = hello() for the compiler to accept it (which is OK as well… but not required).
I should note that I don’t think anyone disagrees that this is bad code and not idiomatic Go. I’m just trying to understand why the compiler doesn’t enforce it because it seems like a simple case that would ensure cleaner and easier to read code. I also know (don’t have an exact specific example) that I’ve copy/pasted function signatures and forgot to handle the error and when the compiler doesn’t complain, I forget. So it’s not about trying to take a short cut so much as trying to catch simple mistakes.