-2

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?

I159
  • 29,741
  • 31
  • 97
  • 132
user1495948
  • 65
  • 2
  • 9

2 Answers2

3

Use the following sinppet:

myval := LEV_ERROR

or

var myval ErrorVal = LEV_ERROR
sadlil
  • 3,077
  • 5
  • 23
  • 36
0

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)
}

Go by Example:

We can use iota to simulate C's enum or #define constant.

I159
  • 29,741
  • 31
  • 97
  • 132