初級 › slice bounds out of range の原因と直し方 › パターン2
slice bounds out of range の原因と直し方
スライスをs[a:b]の形で切り出す際、範囲の終端が容量を超えているとパニックします。
1 package main 2 3 import "fmt" 4 5 func main() { 6 nums := []int{1, 2} 7 fmt.Println(nums[1:__]) ^ 8 }
panic: runtime error: slice bounds out of range [:5] with capacity 2capacity 2
空欄を埋めて実行を通す
容量2のスライスに対して終端5を指定しています。存在する分だけに丸められることはありません。
次の問題