Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why are there no enums in Go? #17205

Closed
RaeesBhatti opened this issue Sep 23, 2016 · 5 comments
Closed

Why are there no enums in Go? #17205

RaeesBhatti opened this issue Sep 23, 2016 · 5 comments

Comments

@RaeesBhatti
Copy link

What version of Go are you using and what is your operating system and processor architecture?

go version go1.7.1 darwin/amd64

What did you do?

Tried to declare enums in my package.

What did you expect to see?

That there would be some mechanism to declare enums types.

What did you see instead?

That there was no native way to declare enum types in Go lang instead I saw some weird hacks to make things work barely.

What is your question?

Why is there no native way to declare enum types in Go lang, it is such a useful and convenient feature.
Here are enums in HackLang for example.

@RaeesBhatti
Copy link
Author

It would be really nice to have something like:

enum HTTP struct {
    GET   string    "GET"
    POST            "POST"
    DELETE          "DELETE"
    PUT             "PUT"
}

@ALTree
Copy link
Member

ALTree commented Sep 23, 2016

Hi! The Go project does not uses the github issue tracker for asking questions. In fact, there is no "What is you question?" header in the issue template.

See the Questions wiki page for a list of places where you could ask about enums.

@stemar94
Copy link

type HTTP string

const (
    GET    = HTTP("GET")
    POST   = HTTP("POST")
    DELETE = HTTP("DELETE")
    PUT    = HTTP("PUT")
)

@RaeesBhatti
Copy link
Author

@stemar94 But you can't use this to verify type, e.g. in Type struct:

type Request struct {
    Method    HTTP    `json:"method"`
}

It will not make sure that values are one of the consts. Any ideas?

@bradfitz
Copy link
Contributor

Yeah, let's discuss this on the mailing list or Slack instead. See https://golang.org/wiki/Questions

@golang golang locked and limited conversation to collaborators Sep 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants