代码如下:
```go
func mkRand50(n int64) []int {
rand50 := make([]int, 50)
rand.Seed(n)
for i := 0; i < 50; i++ {
rand50[i] = rand.Intn(1000)
}
return rand50
}
func main() {
rand50 := mkRand50(time.Now().Unix())
for i := 0; i < len(rand50); i++ {
if rand50[i] > rand50[1] {
rand50[i], rand50[i+1] = rand50[i+1], rand50[i]
}
}
}
```
错误信息如下:
panic: runtime error: index out of range [50] with length 50
goroutine 1 [running]:
main.main()
d:/GOPATH/src/LearnGo/a7/main.go:52 +0x28c
exit status 2
更多评论
for i := 0; i < len(rand50) -1 ; i++ {
if rand50[i] > rand50[1] {
rand50[i], rand50[i+1] = rand50[i+1], rand50[i]
}
}
#2