dongsuishou8039 2013-06-13 11:11
浏览 123
已采纳

去正确的行为,还是编译器错误?

package main

type Key struct {
    stuff1 string
    stuff2 []string
}

type Val struct {
}

type MyMap struct {
    map1 map[Key]*Val // compiles fine!
}

func main() {
    var map2 map[Key]*Val // "invalid map key type Key"
}

Is this the correct behaviour, or a bug in go compiler?

I am using go-1.1 on Linux x64.

  • 写回答

1条回答 默认 最新

  • douzuqin3467 2013-06-13 11:17
    关注

    The compiler is right. From the specs: Map Types:

    The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice.

    This restriction applies transitively if the key type is a struct to all of the struct fields, they must obey the above quoted rule as well, which

    stuff2 []string
    

    does not.

    EDIT:

    What concerns map1 not being flagged, that probably is a bug, perhaps caused by MyMap never being referenced and thus it's type checking was probably skipped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误