Back to questions
JuniorTheoryOccasional
What does the program print?
package main
import "fmt"
func main() {
var p *int
var v any = p
switch x := v.(type) {
case nil, *int:
fmt.Println(x == nil)
}
}false
- Expecting
true: when acaselists more than one type,xkeeps the typeany, and an interface that holds the type*intis not equal tonil. - Expecting a nil pointer to match no branch:
vholds the dynamic type*int, so it matches*intin the list, notnil.
- What would the program print if the branch were
case *int:alone? - Which branch would
var v anywith no assignment match, and what wouldx == nilprint there?