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