账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    golang的反射引发出类型问题;
    68
    0
    1. 今天看the-way-to-go,关于反射赋值时候一个疑问(11.10.2章节)
    2. 代码如下
    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func main() {
        var x float64 = 3.14
        v := reflect.ValueOf(x)    
        fmt.Println("settability of v:", v.CanSet())
        fmt.Println("v.type:", v.Type())  //这一行输出v.type: float64
        v = reflect.ValueOf(&x)
        fmt.Println("type of v:", v.Type()) //这一行输出type of v: *float64
        fmt.Println("settability of v:", v.CanSet())
        v = v.Elem()
        fmt.Println("the elem of v is:", v)
        fmt.Println("settability of v:", v.CanSet())
        v.SetFloat(3.1415)
        fmt.Println(v.Interface())
        fmt.Println(v)
    }

    其中 v := reflect.ValueOf(x) 中v的类型是float64,为何在v = reflect.ValueOf(&x)之后v的类型是*float64后还能用=赋值,这样前后类型不一样为什么不会出错?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 難以釋懷 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部