gpt4 book ai didi

go - map 范围循环中的空接口(interface)

转载 作者:数据小太阳 更新时间:2023-10-29 03:32:28 25 4
gpt4 key购买 nike

下面的代码没有按预期工作。

package main

import "fmt"

func main() {
questions := make(map[int]interface{})
questions[1] = map[interface{}]string{
"q1": "This is Question - 1?",
"op1": "This is Option - 1",
"op2": "This is Option - 2",
true: "This is Option - 1",
}

// This give map[interface {}]string
fmt.Printf("%T \n", questions[1])

// This not working
for key, val := range questions[1] {
printf("%v : %v", key, val)
}


}

遍历 map 的 for 循环不工作。

Go 编译器给出错误“无法范围覆盖问题[1](类型接口(interface){})”

最佳答案

理解 questions[1] 类型是 map

for key, val := range questions[1].(map[interface{}]string) {
fmt.Printf("%v : %v\n", key, val)
}

关于go - map 范围循环中的空接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com