4 ms·
> In Go 1.1, an integer division by constant zero is not a legal program, so it is a compile-time error How often do be people accidentally divide by a constan
by ubershmekel 14y ago
> In Go 1.1, an integer division by constant zero is not a legal program, so it is a compile-time error
How often do be people accidentally divide by a constant 0? I personally use that to simulate runtime errors all the time in python. It's shorter than writing "raise Exception()".
- duaneb 14y agoIt's possibly easier to specifically handle that corner case.
- laumars 14y ago> How often do be people accidentally divide by a constant 0? As much as I do love the level of checking that Go's compiler does, I must admit I asked myself that same question when I read the article.
- dsymonds 14y agoGo has a builtin function called panic, which is much more useful and well-defined. panic("simulated runtime error")
- verytrivial 14y agoWhat about programs that write programs that divide by zero?
- yebyen 14y agoIf those programs (the second level of programs) are compiled with the go compiler too, then they will also be caught (and sacked.)
- IanCal 14y ago> How often do be people accidentally divide by a constant 0? Depends how far the compiler goes to simplify constant expressions. It could be quite useful if it detects something like: f(x,y) : x / (x - (2*y)) f(6,3)
- jsmeaton 14y agoI'm not familiar with Go, but I'd imagine something like this: const int SomeNumber = 0; // then much later in the program return 3 / SomeNumber; An accidental and not immediately obvious divide by constant 0.
- JulianMorrison 14y agoconst weasels = 5 const lawyers = 5 const mustelids = weasels - lawyers feedWeaselsEach(chickenPortions / mustelids)
- netcraft 14y agowould this count as a constant 0 and be caught by the compiler?
- JulianMorrison 14y agoYup. Constants are computed at compile time in Go.
- enneff 14y agoThis change was added to ratify an assumption that the compiler had already made. As has been pointed out, this is not a big deal. Almost nobody is affected, but our release notes must be comprehensive.