Iota is an identifier which is used with constant and which can simplify constant definitions that use auto increment numbers. The IOTA keyword represent integer constant starting from zero. So essentially it can be used to create effective constant in Go .
Iota in Go is used to represent constant increasing sequences. When repeated in a constant, its value gets incremented after each specification.
To declare a constant and give it a name, the const keyword is used. Constants cannot be declared using the := syntax.
An enum, or enumerator, is a data type consisting of a set of named constant values. Enums are a powerful feature with a wide range of uses. However, in Golang, they're implemented quite differently than most other programming languages.
I have the following type defined using iota in Golang.
type StatusType int
const (
PENDING StatusType = iota
APPROVED
REJECTED
)
I want to restrict the value passed in REST-API to the StatusType. Such that the value should not exceed 0,1,2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With