Search⌘ K
AI Features

Puzzle 24 Explanation: Go Flag

Understand how the Go compiler treats the Flag type in Puzzle 24 and why type assertions can cause panics. Learn to use the comma ok idiom for safe type assertions and see how changing Flag to a type alias affects type safety. This lesson helps improve your understanding of Go's type system and error handling.

We'll cover the following...

Try it yourself

Try executing the code below to see the result for yourself.

Go (1.16.5)
package main
import (
"fmt"
)
type Flag int
func main() {
var i interface{} = 3
f := i.(Flag)
fmt.Println(f)
}

Explanation

Even ...