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

huangapple go评论85阅读模式
英文:

undefined (cannot refer to unexported field or method)

问题

我正在尝试从models包中引用Users struct,并尝试从control中访问该模型。但是我遇到以下错误。

controllers/user.go:87: user.create_date未定义(无法引用未导出的字段或方法create_date)
controllers/user.go:88: user.update_date未定义(无法引用未导出的字段或方法update_date)
controllers/user.go:104: user.user_id未定义(无法引用未导出的字段或方法user_id)
controllers/user.go:119: user.update_date未定义(无法引用未导出的字段或方法update_date)
controllers/user.go:136: user.user_id未定义(无法引用未导出的字段或方法user_id)
controllers/user.go:151: user.update_date未定义(无法引用未导出的字段或方法update_date)
controllers/user.go:166: user.user_id未定义(无法引用未导出的字段或方法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"`
    
    	}

在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("创建用户时出错,错误为'%v'", err)
            res := gin.H{
                    "status": "403",
                    "error": "无法创建用户",
            }
            c.JSON(404, res)
            return
        }
    
        content := gin.H{
                "status": "201",
                "result": "成功",
                "UserID": user.user_id,
            }
    
      c.Writer.Header().Set("Content-Type", "application/json")
      c.JSON(201, content)
    }

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

  c.Bind(&user)

    // 更新时间戳
    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("更新用户时出错,错误为'%v'", err)
        res := gin.H{
                "status": "403",
                "error": "无法更新用户",
        }
        c.JSON(403, res)
        return
    }

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

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

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

  c.Bind(&user)

    // 更新时间戳
    user.update_date = time.Now()

    err := ac.DB.Where("user_id = ?", id).Delete(&user)
    if err != nil {
        loguser.Debugf("删除用户时出错,错误为'%v'", err)
        res := gin.H{
                "status": "403",
                "error": "无法删除用户",
        }
        c.JSON(403, res)
        return
    }

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

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

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

得分: 22

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

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 来获取字段。

英文:

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.

huangapple
  • 本文由 发表于 2016年3月5日 15:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/35811215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定