初級 › cannot use ... as ... value(型の不一致)の原因と直し方 › パターン2
cannot use ... as ... value(型の不一致)の原因と直し方
変数の型と代入しようとする値の型が食い違うと、コンパイル時にcannot useエラーになります。
1 package main 2 3 import "fmt" 4 5 func main() { 6 var age int = ____ ^ 7 fmt.Println(age) 8 }
./main.go:6:19: cannot use "20" (untyped string constant) as int value in variable declaration
空欄を埋めて実行を通す
数字に見える文字列"20"でも、Goはint型への暗黙変換を行いません。クォートを外した整数リテラルにする必要があります。
次の問題