zl程序教程

您现在的位置是:首页 >  后端

当前栏目

2022-09-18:以下go语言代码输出什么?A:1;B:15;C:panic index out of range;D:doesn’t compile。 p

Go输出语言代码 什么 2022 of index
2023-06-13 09:12:47 时间

2022-09-18:以下go语言代码输出什么?A:1;B:15;C:panic index out of range;D:doesn’t compile。

package main

import (
  "fmt"
)

func main() {
  nums := []int{1, 2, 3, 4, 5}
  sum := 0
  for i, n := range nums {
    i = 6
    sum += n
  }
  fmt.Println(sum)
}

答案选D。这道题很多人会选B。i处于未使用状态,编译不会通过。

在这里插入图片描述