

Go Enum
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. In Golang, we use a predeclared identifier, iota, and the enums are not strictly typed.Example of Go Enum
package main
import "fmt"
type Direction int
const (
North Direction = iota
South
East
West
)
func main() {
var myDirection Direction
myDirection = West
if (myDirection == West) {
fmt.Println("myDirection is West:", myDirection)
}
}
Output
myDirection is West: 3
Passing Enum to function as Parameter in Go
package main
import "fmt"
type Direction int
const (
North Direction = iota
South
East
West
)
func printDirection(myDirection Direction){
if (myDirection == West) {
fmt.Println("myDirection is West:", myDirection)
}
}
func main() {
var myDirection Direction
myDirection = West
printDirection(myDirection)
}
Output
myDirection is West: 3
Conclusion
In this page (written and validated by A. Gawali) you learned about Go Enum . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Json Marshal Unmarshal.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.
Share On: |
Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.