中級 › interface conversion panic の原因と直し方 › パターン2
interface conversion panic の原因と直し方
型アサーション v.(T) は、vの実際の型がTでない場合に実行時パニックを起こします。
1 package main 2 3 import "fmt" 4 5 func main() { 6 var i interface{} = ____ ^ 7 n := i.(int) 8 fmt.Println(n) 9 }
panic: interface conversion: interface {} is bool, not intiの実際の型はbool
空欄を埋めて実行を通す
boolを格納したinterfaceに対してintへの型アサーションを行うと、実際の型がbool, not intとしてパニックします。
次の問題