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