这是代码:
// User Model
type User struct {
UserID int `db:"user_id"`
UserNme string `db:"user_nme"`
UserEmail string `db:"user_email"`
UserAddressID sql.NullInt64 `db:"user_address_id"`
}
func (ur *userRepository) FindAll() ([]models.User, error) {
var users []models.User
query := "select user_nme from users"
err := ur.Db.Select(&users, query)
if err != nil {
return nil, err
}
return users, nil
}
结果:
&[]models.User{models.User{UserID:0, UserNme:"Jay Durgan", UserEmail:"", UserAddressID:sql.NullInt64{Int64:0, Valid:false}}, models.User{UserID:0, UserNme:"Arne Balistreri", UserEmail:"", UserAddressID:sql.NullInt64{Int64:0, Valid:false}}, models.User{UserID:0, UserNme:"Greg Willms", UserEmail:"", UserAddressID:sql.NullInt64{Int64:0, Valid:false}}, models.User{UserID:0, UserNme:"Lady Aisha McLaughlin", UserEmail:"", UserAddressID:sql.NullInt64{Int64:0, Valid:false}}, models.User{UserID:0, UserNme:"Mrs. Phoebe Boyle", UserEmail:"", UserAddressID:sql.NullInt64{Int64:0, Valid:false}}}%
正如您所看到的,我没有查询user_id,user_email和user_address_idcolumns ,但结果给我这些字段的值为零。
那么,有没有办法只获取与查询列对应的字段呢?另外,我不想这样写:&user.userNme, &user.xxx,&user.xxx这意味着编写每个字段并填充它。太冗长了。
预期结果是:{UserNme: "Jay Durgan"}...
牧羊人nacy
开心每一天1111
相关分类