douwei7203 2015-02-17 15:24
浏览 338
已采纳

使用下划线从golang结构访问mysql字段

I'm working with the https://github.com/jinzhu/gorm library. For some reason I can't access some fields that I expect to be there. Those particular fields have underscores in them. For instance when I try to Access SpeName it doesn't fail but it also does not give me a string

  1. type Specialties struct {
  2. SpeId int64
  3. SpeName string
  4. Conditions sql.NullString
  5. ParentId sql.NullInt64
  6. Hidden sql.NullInt64
  7. }
  8. func IsFolderNameASpecialty(folderName string) models.Specialties {
  9. var sSpecialty models.Specialties
  10. for _, specialty := range Specialties {
  11. fmt.Println(strings.ToLower(folderName), specialty.SpeName)
  12. if strings.ToLower(folderName) == strings.ToLower(specialty.SpeName) {
  13. sSpecialty = specialty
  14. }
  15. }
  16. return sSpecialty
  17. }

For Some reason the println is empty. The mysql field is really spe_Name instead of SpeName. What tag do I use to allow me to access the field properly?

  • 写回答

1条回答 默认 最新

  • dongzan2740 2015-02-17 15:41
    关注

    According to https://github.com/jinzhu/gorm#existing-schema, you want to use column: in the struct type tag.

    type Specialties struct {
        // ...
        SpeName    string `gorm:"column:spe_Name"`
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部