duanke0555 2016-11-22 09:37
浏览 25
已采纳

如何标记无法修改的嵌入式结构的字段?

Suppose there is an external library libA who declares NotMyType.

type NotMyType struct {
  NotMyField string
}

And you would like to embed it with one of your own types, which you use with an ORM, which uses the tags to adjust column properties.

type MyType struct {
  SomeData0 string  `orm:"nullable"`
  SomeData1 string  `orm:"nullable"`
  libA.NotMyType
}

For example, columns MyType.SomeData0 and MyType.SomeData1 are NULLABLE.

Is it possible to tag NotMyField with orm:"nullable" without modifying NotMyType?

  • 写回答

2条回答 默认 最新

  • doutao5499 2016-11-22 10:01
    关注

    EDIT: wrong answer, my bad

    Okep i think i got it. I have never used reflect before so maybe i'm wrong. But :

    Take a look at what I just do :

    https://play.golang.org/p/U0uyomL-It

    I have a struct

    type User struct {
        name string
        age  int
    }
    

    With no tag.

    I get the field with

    field, ok := reflect.TypeOf(user).Elem().FieldByName("name")
    if !ok {
        panic("Field not found")
    }
    

    And i set a new tag with

    //setStructTag(&field)
    
    func setStructTag(f *reflect.StructField) {
        f.Tag = "`json:name-field`"
    }
    

    Hope my researche will help you :)

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料