duanpo6079 2016-03-05 07:05
浏览 57
已采纳

未定义(无法引用未导出的字段或方法)

I'm trying to refer Users struct from the models package and trying to access the model from control.But I take following errors.

controllers/user.go:87: user.create_date undefined (cannot refer to unexported field or method create_date)
controllers/user.go:88: user.update_date undefined (cannot refer to unexported field or method update_date)
controllers/user.go:104: user.user_id undefined (cannot refer to unexported field or method user_id)
controllers/user.go:119: user.update_date undefined (cannot refer to unexported field or method update_date)
controllers/user.go:136: user.user_id undefined (cannot refer to unexported field or method user_id)
controllers/user.go:151: user.update_date undefined (cannot refer to unexported field or method update_date)
controllers/user.go:166: user.user_id undefined (cannot refer to unexported field or method user_id)

Models.go

package models


import(

    "time"
)

    type Users struct {

            user_id                                      int      `json:"user_id" form:"user_id" gorm:"column:user_id"`
            user_login                                   string   `json:"user_login" form:"user_login" gorm:"column:user_login"` 
            user_email                                   string   `json:"user_email" form:"user_email" gorm:"column:user_email"` 
            user_password                                string   `json:"user_password" form:"user_password" gorm:"column:user_password"` 
            user_password_salt                           string   `json:"user_password_salt" form:"user_password_salt" gorm:"column:user_password_salt"` 
            user_2factor_secret                          string   `json:"user_2factor_secret" form:"user_2factor_secret" gorm:"column:user_2factor_secret"`
            user_fullname                                string   `json:"user_fullname" form:"user_fullname" gorm:"column:user_fullname"`
            user_description                             string   `json:"user_description" form:"user_description" gorm:"column:user_description"`
            user_enabled                                 string   `json:"user_enabled" form:"user_enabled" gorm:"column:user_enabled"`
            user_verified                                string   `json:"user_verified" form:"user_verified" gorm:"column:user_verified"`
            PublisherInfoID                              int      `json:"PublisherInfoID" form:"PublisherInfoID" gorm:"column:PublisherInfoID"`
            DemandCustomerInfoID                         int      `json:"DemandCustomerInfoID" form:"DemandCustomerInfoID" gorm:"column:DemandCustomerInfoID"`
            create_date                                  time.Time `json:"create_date" gorm:"column:create_date"`
            update_date                                  time.Time  `json:"update_date" gorm:"column:update_date"` 
            user_permission_cache                        string   `json:"user_permission_cache" form:"user_permission_cache" gorm:"column:user_permission_cache"`
            user_role                                    int      `json:"user_role" form:"user_role" gorm:"column:user_role"`

        }

in controllers

   package controllers

    import (
        "time"

      "github.com/op/go-logging"
        "github.com/gin-gonic/gin"
        "github.com/jinzhu/gorm"
      _ "github.com/go-sql-driver/mysql"

        "../models"
    )

    var loguser = logging.MustGetLogger("AdsAPI")

    type AdsControllerUser struct {
        DB gorm.DB
    }

    func (ac *AdsControllerUser) SetDB(d gorm.DB) {
        ac.DB = d
        ac.DB.LogMode(true)
    }
    func (ac *AdsControllerUser) CreateUsers(c *gin.Context) {

      var user models.Users

      // This will infer what binder to use depending on the content-type header.
      c.Bind(&user)

        // Update Timestamps
        user.create_date = time.Now()
        user.update_date = time.Now()

        err := ac.DB.Save(&user)
        if err != nil {
            loguser.Debugf("Error while creating a user, the error is '%v'", err)
            res := gin.H{
                    "status": "403",
                    "error": "Unable to create user",
            }
            c.JSON(404, res)
            return
        }

        content := gin.H{
                "status": "201",
                "result": "Success",
                "UserID": user.user_id,
            }

      c.Writer.Header().Set("Content-Type", "application/json")
      c.JSON(201, content)
    }

func (ac *AdsControllerUser) UpdateUsers(c *gin.Context) {
    // Grab id
    id := c.Params.ByName("id")
  var user models.Users

  c.Bind(&user)

    // Update Timestamps
    user.update_date = time.Now()

    //err := ac.DB.Model(&models.auth_User).Where("user_id = ?", id).Updates(&cm)
    err := ac.DB.Where("user_id = ?", id).Updates(&user)
    if err != nil {
        loguser.Debugf("Error while updating a user, the error is '%v'", err)
        res := gin.H{
                "status": "403",
                "error": "Unable to update user",
        }
        c.JSON(403, res)
        return
    }

    content := gin.H{
            "status": "201",
            "result": "Success",
            "UserID": user.user_id,
        }

    c.Writer.Header().Set("Content-Type", "application/json")
    c.JSON(201, content)
}

func (ac *AdsControllerUser) DeleteUsers(c *gin.Context) {
    // Grab id
    id := c.Params.ByName("id")
  var user models.Users

  c.Bind(&user)

    // Update Timestamps
    user.update_date = time.Now()

    err := ac.DB.Where("user_id = ?", id).Delete(&user)
    if err != nil {
        loguser.Debugf("Error while deleting a user, the error is '%v'", err)
        res := gin.H{
                "status": "403",
                "error": "Unable to delete user",
        }
        c.JSON(403, res)
        return
    }

    content := gin.H {
            "result": "Success",
            "UserID": user.user_id,
        }

  c.Writer.Header().Set("Content-Type", "application/json")
  c.JSON(201, content)
}
  • 写回答

1条回答 默认 最新

  • dongzuo7166 2016-03-05 07:14
    关注

    Use Capitals for exported fields in struct, when referring struct in another package.

    package models
    
    import (
        "time"
    )
    
    type Users struct {
        ID                   int       `json:"user_id" form:"user_id" gorm:"column:user_id"`
        Login                string    `json:"user_login" form:"user_login" gorm:"column:user_login"`
        Email                string    `json:"user_email" form:"user_email" gorm:"column:user_email"`
        Password             string    `json:"user_password" form:"user_password" gorm:"column:user_password"`
        PasswordSalt         string    `json:"user_password_salt" form:"user_password_salt" gorm:"column:user_password_salt"`
        TwoFactorSecret      string    `json:"user_2factor_secret" form:"user_2factor_secret" gorm:"column:user_2factor_secret"`
        Fullname             string    `json:"user_fullname" form:"user_fullname" gorm:"column:user_fullname"`
        Description          string    `json:"user_description" form:"user_description" gorm:"column:user_description"`
        Enabled              string    `json:"user_enabled" form:"user_enabled" gorm:"column:user_enabled"`
        Verified             string    `json:"user_verified" form:"user_verified" gorm:"column:user_verified"`
        PublisherInfoID      int       `json:"PublisherInfoID" form:"PublisherInfoID" gorm:"column:PublisherInfoID"`
        DemandCustomerInfoID int       `json:"DemandCustomerInfoID" form:"DemandCustomerInfoID" gorm:"column:DemandCustomerInfoID"`
        CreateDate           time.Time `json:"create_date" gorm:"column:create_date"`
        UpdateDate           time.Time `json:"update_date" gorm:"column:update_date"`
        PermissionCache      string    `json:"user_permission_cache" form:"user_permission_cache" gorm:"column:user_permission_cache"`
        Role                 int       `json:"user_role" form:"user_role" gorm:"column:user_role"`
    }
    

    Now do Users.ID to get fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程