4 ms·
fmt.Fprint(os.Stdout, "Something\n") By itself isn't a valid statement. You try to compile that and you'll get an error saying unused return value. So you'll
by Tobani 12y ago
fmt.Fprint(os.Stdout, "Something\n")
By itself isn't a valid statement. You try to compile that and you'll get an error saying unused return value. So you'll try:
err := fmt.Fprint(os.Stdout, "Something\n")
and you could ignore err, but then you'll get a compiler saying err is defined but never used. So you can be like NAH I'm just trying to muck stuff up so I"m going to do this.
_ = fmt.Fprint(os.Stdout, "Something\n")
Which is explicitly discarding the error much like:
try{
fmt.Fprint(os.Stdout, "Something\n")
}catch(Throwable t){}
- robryk 12y agoI'm sorry, but that's not true: http://play.golang.org/p/PiMXQkKOef http://play.golang.org/p/PiMXQkKOef
- Tobani 12y agoSo it appears you're right. For things for which you don't care about any value returned this is the case. The reason I haven't run into this is for the most part things that can error, they also return a value that matters. You can't use the return value without doing something with the error. http://play.golang.org/p/yreUbxx5Vv http://play.golang.org/p/yreUbxx5Vv So it appears that there is a loophole for things that can error and purely used for side effects.
- NateDad 12y agoThis is true, you can drop all return values. However, a lot of functions return a value and an error. If you want to access the value, you need to assign the error to something too. And if you don't then use that error, the compiler will complain about it. For simply dropping all return values on the ground, there are linters that will complain for you: https://github.com/kisielk/errcheck https://github.com/kisielk/errcheck