azev
V2EX  ›  问与答

想获取 gorm model struct 中字段的数据库列名

  •  
  •   azev · Aug 29, 2022 · 1443 views
    This topic created in 1379 days ago, the information mentioned may be changed or developed.
    类似 Province string `gorm:"primaryKey;column:Region" json:"description"`
    现在是拿取 tag 中的 gorm 部分 这个发现有 Get 方法比较好拿到
    问题是这个字符串解析最好是怎么来?我想的是用正则
    但不知道是不是最好的办法?
    4 replies    2022-08-29 11:22:28 +08:00
    Morriaty
        2
    Morriaty  
       Aug 29, 2022
    通过反射拿
    // var element reflect.Type
    tag := element.Field(i).Tag.Get("gorm")
    azev
        3
    azev  
    OP
       Aug 29, 2022
    @Morriaty
    @lvluo200711 我是已经拿到了内容"primaryKey;column:Region" 现在是需要解析出列名
    2024
        4
    2024  
       Aug 29, 2022
    @azev 如果执行过程动态,二楼的就好了,剩下的就是字符串切割(; )+ (:) 就立刻出来啦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   959 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 21:54 · PVG 05:54 · LAX 14:54 · JFK 17:54
    ♥ Do have faith in what you're doing.