doujianglin6704 2018-10-27 19:28
浏览 19
已采纳

如果地图中未包含某些内容,则返回值取什么值?

Ok so according to this:

How to check if a map contains a key in go?

if val, ok := m["foo"]; ok {
    //do something here
}

that's fine, but how come we can't do this:

val, ok := m["foo"]

if val == nil {   // cannot compare val to nil

}

I get a compilation error saying I can't compare val to nil, but then what value does val have? What can I compare it to, to determine if it exists or not?

the type of m is like:

type m map[string]struct{}
  • 写回答

2条回答 默认 最新

  • doubiao7410 2018-10-27 19:34
    关注

    The Go Programming Language Specification

    Index expressions

    For a of map type M: if the map is nil or does not contain such an entry, a[x] is the zero value for the element type of M.

    The zero value

    When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.


    The Go Programming Language Specification

    Composite literals

    Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated. They consist of the type of the literal followed by a brace-bound list of elements. Each element may optionally be preceded by a corresponding key. For struct literals the following rules apply:

    A literal may omit the element list; such a literal evaluates to the zero value for its type.

    For your example, type struct{}, omit the element list from the composite literal, struct{}{}, for the zero value.

    For example,

    package main
    
    import "fmt"
    
    func main() {
        m := map[string]struct{}{}
        val, ok := m["foo"]
        fmt.Printf("%T %v
    ", val, val)
        if val == struct{}{} {
            fmt.Println("==", val, ok)
        }
    }
    

    Playground: https://play.golang.org/p/44D_ZfFDA77

    Output:

    struct {} {}
    == {} false
    

    The Go Programming Language Specification

    Variable declarations

    A variable declaration creates one or more variables, binds corresponding identifiers to them, and gives each a type and an initial value.

    If a list of expressions is given, the variables are initialized with the expressions following the rules for assignments. Otherwise, each variable is initialized to its zero value.

    If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment.

    In your example, you could declare a variable of type struct{} with no initial value, which would be initialized to the zero value for the struct{} type.

    For example,

    package main
    
    import "fmt"
    
    func main() {
        m := map[string]struct{}{}
        val, ok := m["foo"]
        fmt.Printf("%T %v
    ", val, val)
        var zeroValue struct{}
        if val == zeroValue {
            fmt.Println("==", val, ok)
        }
    }
    

    Playground: https://play.golang.org/p/_XcSCEeEKJV

    Output:

    struct {} {}
    == {} false
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义