我试图从模型包中引用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)
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"`
}
控制器中的
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)
}
答案 0 :(得分:17)
在另一个包中引用struct时,将struct用于struct中的导出字段。
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
来获取字段。