Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
var v any = 42
s, ok := v.(string)
fmt.Println(len(s), ok)
}0 false
- Expecting
2 true: a type assertion only checks the type stored inv; it never converts42into the string"42". - Expecting a panic: only the one-result form
s := v.(string)panics; with, oka failed assertion setsoktofalseandsto"".
- What does the program print if the assertion is changed to
v.(int)? - How would a type switch on
vhandle bothintandstring?