初級 › invalid operation: mismatched types string and int の原因と直し方 › パターン3
invalid operation: mismatched types string and int の原因と直し方
文字列と数値を+演算子でそのまま連結しようとすると、型が一致していないためコンパイルエラーになります。
1 package main 2 3 import ( 4 "fmt" 5 "strconv" 6 ) 7 8 func main() { 9 msg := "total: " 10 amount := 100 11 fmt.Println(__________________________) ^ 12 }
./main.go:11:14: invalid operation: msg + amount (mismatched types string and int)
空欄を埋めて実行を通す
Goは暗黙の型変換を行わないため、+演算子の両辺は同じ型でなければなりません。
次の問題