0

Let's say I have something like this:

const (
    FOO int = iota
    BAR
    BAZ
)

And I can access to variables by FOO, BAR and so on. But storing a big amount of constants in one namespace isn't good so I'm trying to hide this enum in structure or something like that so I can get value by typing actions.FOO in same namespace. I've tried many ways but didn't find anything like that. I would like to mention that easiest workaround, in this case, will be anonymous structure but I wanna keep auto indexing with iota.

5
  • 1
    Possible duplicate of GOLANG "Namespaced" enums? Commented Aug 26, 2016 at 17:34
  • I've seen this question but in my case, I'm writing all code in one namespace. Commented Aug 26, 2016 at 17:36
  • Only possible solutions I can think of are the answers in the question mentioned by Tim. Commented Aug 26, 2016 at 17:49
  • @AJPennster, according to answers there is only way is to store enums in another package, is there is no way of storing enumerable in same namespace as property of structure for example? Commented Aug 26, 2016 at 17:56
  • There is a second answer there with the use of types. The constants will still be globally available to the package but only if you use the type associated with it. That is the closest answer I can think of to what you're asking. Commented Aug 26, 2016 at 18:14

1 Answer 1

0

The only way to assign some sort of enumerable behind the property without creating the separate package that I found is to use anonymous structure.

type someType int

var ConstantsList = struct {
   FOO, BAR, BAZ someType
}{1, 2, 3}

There are few downsides of using it, it's not immutable, and doesn't have auto increment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.