Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I generally tend to use enumer[0] to generate some boilerplate code that can help with addressing this, e.g. the below would compile, but would error at runtime. There are probably linters out there that could catch this. With Go, linters are generally pretty good at catching this kind of stuff.

    package main
    
    import "fmt"
    
    type Test int
    
    const (
     T1 Test = 0
     T2      = 1
    )
    
    func main() {
     t, err := TestString("T1")
     if err != nil {
      panic(err)
     }
    
     TestSomething(t)
    }
    
    func TestSomething(t Test) {
     fmt.Println(t.String())
    }
Having said that, it seems weird to have to mimic enums, as opposed to actually having it. Doesn't feel like it would add much complexity, if at all.

[0] https://github.com/dmarkham/enumer



> as opposed to actually having it

Like C or C++ do? :)


Right. However, C and C++ are far more complex, with no memory safety. Everything has it's ups and downs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: