Pardon me my knowledge in Go is very limited. I have a definition like this
type ErrorVal int
const (
LEV_ERROR ErrorVal = iota
LEV_WARNING
LEV_DEBUG
)
Later in my Go sample code I want to define a value for a type of ErrorVal
.
What I am trying to do is in C we can define enum value like this
enum ErrorVal myVal = LEV_ERROR;
How can I do something similar in Go?
Use the following sinppet:
myval := LEV_ERROR
or
var myval ErrorVal = LEV_ERROR
You can assign a constant to a variable and get the same result as C's enum
:
type ErrorVal int
const (
LEV_ERROR ErrorVal = iota
LEV_WARNING
LEV_DEBUG
)
func main() {
myval := LEV_ERROR
fmt.Println(myval)
}
We can use iota to simulate C's enum or #define constant.