duanmao9918 2017-10-11 03:47
浏览 68
已采纳

golang为什么命名和未命名结构比较的结果相同

The Go Programming Language Specification said.

Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

But as following code snippet, it seems variable v1, and v3 has different type, why they can get a true output:

package main

import "fmt"
import "reflect"

type T1 struct { name string }
type T2 struct { name string }

func main() {
    v1 := T1 { "foo" }
    v2 := T2 { "foo" }
    v3 := struct{ name string } {"foo"}
    v4 := struct{ name string } {"foo"}

    fmt.Println("v1: type=", reflect.TypeOf(v1), "value=", reflect.ValueOf(v1)) // v1: type= main.T1 value= {foo}
    fmt.Println("v2: type=", reflect.TypeOf(v2), "value=", reflect.ValueOf(v2)) // v2: type= main.T2 value= {foo}
    fmt.Println("v3: type=", reflect.TypeOf(v3), "value=", reflect.ValueOf(v3)) // v3: type= struct { name string } value= {foo}
    fmt.Println("v4: type=", reflect.TypeOf(v4), "value=", reflect.ValueOf(v4)) // v4: type= struct { name string } value= {foo}

    //fmt.Println(v1 == v2) // compiler error: invalid operation: v1 == v2 (mismatched types T1 and T2)
    fmt.Println(v1 == v3)   // true, why? their type is different
    fmt.Println(v2 == v3)   // true, why?
    fmt.Println(v3 == v4)   // true
}

It's reasonable that v1 == v2 fails with compile error because they are different type, however how to explain the v1 == v3 get a true result, since they also have different types, one with named struct type T1, and the other with anonymous struct. Thanks.

Update Question based on feedback

Thanks @icza, @John Weldon for your explanation, I think this issue is resolved, I am now updating the question.

In summary, a struct is comparable if it meet following 2 specs:

  1. Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

  2. In any comparison, the first operand must be assignable to the type of the second operand, or vice versa.

The 1st one is for struct type variable specific; and the 2nd one is for all types variable comparison, struct type variable is covered of course.

In my sample, the comparison variable v1 and v3 meet these two specs definition.

  1. All fields are comparable; in fact, the first spec define the struct rule, it focus on fields, but not struct itself, so whatever a named struct or anonymous struct, they are same rule.
  2. Variable v1 and v3 is assignable. (according to the rule: x's type V and T have identical underlying types and at least one of V or T is not a defined type)

So this is to explain why "v1 == v3" can get a true result. Thanks all.

  • 写回答

2条回答 默认 最新

  • duanhao7786 2017-10-11 04:05
    关注

    If you read that spec carefully, you can see that if the corresponding non-blank fields are equal then the two structs are equal. If the type name is different the compiler will fail, but if one or both of the types are anonymous then they'll be comparable. The types T1 or T2, and the anonymous structs are effectively the same type because they have the same fields. When the field values are the same then they compare as the same.

    Looking at type identity in the spec may (or may not, ymmv) make it clearer.

    Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags. Non-exported field names from different packages are always different.

    So, if you try the same experiment but change the types by adding field tags, or by putting the types in different packages, you may get the differences you expect.

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名