douliao5942 2016-08-18 13:05
浏览 433
已采纳

v.Type()。Elem()和v.Elem()。Type()有什么区别?

In the following code, both options seem to allocate the same resource

func Allocate(v interface{}) error {
    rv := reflect.ValueOf(v)
    if rv.IsNil() {
        return errors.New("Value of v is nil")
    }
    s0 := reflect.New(rv.Type().Elem())
    s1 := reflect.New(rv.Elem().Type())

    return errors.New(fmt.Sprintf("What's the diff? %v %v", s0, s1))
}

If there's no difference in this specific example, an example illustrating the difference will be great. Also what's the preferable option in this specific case when trying to allocate for an interface.

Edit: reflect.DeepEqual(s0, s1) returns false. I think rv.Elem().Type() has a problem dealing with zero values so maybe rv.Type().Elem() is preferred.

Playground

  • 写回答

1条回答 默认 最新

  • douzou7012 2016-08-18 15:33
    关注

    There is no difference if v is a non-nil pointer type.

    s := "hello"
    rv := reflect.ValueOf(&s)
    fmt.Println(rv.Type().Elem() == rv.Elem().Type()) // prints "true"
    

    Here are some examples where rv.Type().Elem() and rv.Elem().Type()) are different:

    // nil pointer
    var p *string
    rv := reflect.ValueOf(p)
    fmt.Println(rv.Type().Elem()) // prints "string"
    fmt.Println(rv.Elem().Type()) // panic: call of reflect.Value.Type on zero Value
    
    // interface value
    var i interface{} = "hello"
    rv := reflect.ValueOf(&i).Elem()
    fmt.Println(rv.Type())        // prints "interface {}"
    fmt.Println(rv.Elem().Type()) // prints "string"
    fmt.Println(rv.Type().Elem()) // panic: Elem of invalid type
    

    If rv.Type().Elem() is used in Allocate, then the nil check can be removed and the function will work with nil pointer values.

    The call reflect.DeepEqual(s0, s1) returns false because the ptr fields in the values are different. DeepEqual compares unsafe pointers as simple values, not as pointers. This example might help explain what's going on:

    v := "hello"
    rv := reflect.ValueOf(&v)
    s0 := reflect.New(rv.Type().Elem())
    s1 := reflect.New(rv.Elem().Type())
    s2 := reflect.New(rv.Type().Elem())
    s3 := reflect.New(rv.Elem().Type())
    fmt.Println(reflect.DeepEqual(s0, s1)) // prints "false"
    fmt.Println(reflect.DeepEqual(s0, s2)) // prints "false"
    fmt.Println(reflect.DeepEqual(s1, s3)) // prints "false"
    fmt.Println(reflect.DeepEqual(s2, s3)) // prints "false"
    fmt.Println(reflect.DeepEqual(s0.Interface(), s1.Interface())) // prints "true"
    fmt.Println(reflect.DeepEqual(s0.Interface(), s2.Interface())) // prints "true"
    fmt.Println(reflect.DeepEqual(s1.Interface(), s3.Interface())) // prints "true"
    fmt.Println(reflect.DeepEqual(s2.Interface(), s3.Interface())) // prints "true"
    

    As you can see, the reflect.Value comparisons are all false, even when created using the same sequence of calls.

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

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源