TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go Enums Suck

4 pointsby mkl95about 1 year ago

1 comment

foldrabout 1 year ago
&gt;Anywhere that accepts an Operation Enum type will just as happily accept an int.<p>This isn&#x27;t quite true. Operation is a different type to int, and a function that expects an int can&#x27;t be passed an Operation or vice versa. However, Go numeric literals are untyped and Go can interpret an integer literal as a value of type Operation where appropriate. This can give the false impression that int and operation are interchangeable as far as the type system is concerned. Here&#x27;s an illustrative example:<p><pre><code> package main type Foo int const ( FooA Foo = iota FooB ) func main() { var i int = 1 var f Foo = 1 &#x2F;&#x2F; this is ok (literal 1 can represent value of type Foo) f = i &#x2F;&#x2F; not ok i = f &#x2F;&#x2F; not ok (func(f Foo) {})(i) &#x2F;&#x2F; not ok i = FooA &#x2F;&#x2F; not ok (because FooA is a typed constant) }</code></pre>