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条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题