go - 未定义(不能引用未导出的字段或方法)

标签 go

我正在尝试从模型包中引用 Users struct 并尝试从控件访问模型。但出现以下错误。

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)

模型.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"`

        }

在 Controller 中

   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)
}

最佳答案

在引用另一个包中的结构时,对结构中的导出字段使用大写字母。

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"`
}

现在执行 Users.ID 来获取字段。

关于go - 未定义(不能引用未导出的字段或方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811215/

相关文章:

go - 如何使用 go mod vendor 从 GOPATH 复制 deps?

go - 如何在 golang 代码中编译 golang 程序?

html - Golang 中的计数器 html 标签 map 无法正常工作

go - 如何使用 couchbase gocb 检查错误代码?

go - 在一个函数中创建结构以在另一个函数中使用

go - 通过 azure-sdk-for-go 下载 Azure 函数文件

go - 在Go中读取JSON流格式

go - 操作字符串数组中的数据

deployment - 无法将示例 GOLang 应用程序部署到 Heroku

json - 使用自定义 MarshalJSON 更改结构中的 JSON 标签