美文网首页
2019-08-29

2019-08-29

作者: 风寒夜末 | 来源:发表于2019-08-29 09:03 被阅读0次

reflect.TypeOf(s).Elem().FieldByName()

上面的s是任何一种sturct的pointer

type Ads struct {

    Id            int

    Title        string

    Location      string

    Number        string

    Content      string

    Image        string

    Inserted_time string

    Token        string

}

上面是我的某一种struct,我new(Ads)之后就会当作第二个参数传入.

如果现在我想获取到这个指针的任一field的话,好像不行。

reflect.TypeOf(s).FieldByName('Title')这个就会报错。因为传入的是一个指针,现在我不想修改传入的类型。有什么办法解决吗?

解决方法

用reflect.TypeOf(s).Elem().FieldByName()就可以对pointer进行查询了~

相关文章

网友评论

      本文标题:2019-08-29

      本文链接:https://www.haomeiwen.com/subject/gosbectx.html