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

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处于未使用状态,编译不会通过。
在这里插入图片描述

posted @ 2022-09-18 21:35  福大大架构师每日一题  阅读(10)  评论(0编辑  收藏  举报  来源