mmcgrana / gobyexample

Go by Example
https://gobyexample.com
7.16k stars 1.26k forks source link

Incorrect example for enum #533

Closed arnold-parge closed 3 months ago

arnold-parge commented 3 months ago

In the enum example, the description says:

If we have a value of type int, we cannot pass it to transition - the compiler will complain about type mismatch. This provides some degree of compile-time type safety for enums.

If we pass an int to the transition it will allow and not complain.

eg:

func main() {
    ns := transition(0)
    fmt.Println(ns)

    ns2 := transition(ns)
    fmt.Println(ns2)
}

Output:

connected
idle
arnold-parge commented 3 months ago

Oh, you meant we can't do this

func main() {
    n := 0
    ns := transition(n)
    fmt.Println(ns)

    ns2 := transition(ns)
    fmt.Println(ns2)
}

👍