![]() ![]() This is because each const block resets iota, essentially grouping the constants together, regardless of type and the order in which they appear. The only correct fix in this example is to move the new constant out of the enumeration const block and into a new const block. When you understand the ConstSpec, it becomes clearer why this is the case but as greeting isn’t logically part of our enumeration (or even an integer), I would have expected iota to behave differently in this case. Without proper unit tests, this is the kind of subtle bug that could take down a production application, or worse, not take it down. iota has been incremented, so one is left-shifted by 2 instead of 1, throwing all subsequent enum values out. If you said 1 2, you’d now be wrong! The output of this program is now 2 4. What would you expect the program to output now? As it’s important and we want it to be visible, we’ve added it to the top of our const block: package main The iota keyword represents successive integer constants 0, 1, 2, It resets to 0 whenever the word const appears in the source code, and increments after each. Now let’s suppose we need to add another constant. If you said 1 2, you’d be correct! There’s nothing fishy going on here. Programs are constructed from packages, whose properties allow efficient management of dependencies. It is strongly typed and garbage-collected and has explicit support for concurrent programming. Without cheating, what would you expect the following code to output? package main Go is a general-purpose language designed with systems programming in mind. Leverage IOTASCALEs our exceptional skills to build robust, scalable and. I ran into a curious little gotcha with Go’s iota construct today and I wanted to share it with you. It includes all the code needed to build out the database, server, and application. ![]()
0 Comments
Leave a Reply. |