6/19/2023 0 Comments Iota golangI might have a metaprogramming addiction. import ( 'fmt' 'strconv' ) import 'math'. We can use iota to simulate C’s enum or define constant. const ( C0 iota C1 iota C2 iota ) fmt. This is open for extension - we could add more methods on the enum description such as converting the integer value of a Direction to its name, or validating a Direction that we parsed from a config file. Go supports constants of character, string, boolean, and numeric values. 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 const specification. TypeScript enums enum Direction = iota ( ) ![]() ![]() String literals (see addEventListener), which can be typed with TypeScript, but look a bit ugly type Direction = 'north' | 'east' | 'south' | 'west' IOTA provides an automated way to create a enum in Golang. ![]() iota can also start from non-zero number- iota expressions can also be used to start iota from any number const ( a iota + 10 b c ) will output. On the next line, iota value is 2 hence output 248. Currently, the ways we create enums include On the next line iota value is 1 hence the output is 1+45.
0 Comments
Leave a Reply. |