Go 如何检查结构中的bool是否已更改为false或是否已为false

Go 如何检查结构中的bool是否已更改为false或是否已为false,go,boolean,go-gorm,Go,Boolean,Go Gorm,如果发送用户帐户对象而未将睡眠值设置为true,则该值将自动为false我需要知道该值是实际设置为false,还是由于未设置而设置为false type user_account struct { ID string `sql:"type:uuid;default:uuid_generate_v4()"` Gender_Identity_id string `sql:"type:uuid;default:uuid_generate_v4()"` Email

如果发送用户帐户对象而未将睡眠值设置为true,则该值将自动为false我需要知道该值是实际设置为false,还是由于未设置而设置为false

type user_account struct {
ID                 string `sql:"type:uuid;default:uuid_generate_v4()"`
Gender_Identity_id string `sql:"type:uuid;default:uuid_generate_v4()"`
Email              string
Name               string
LastName           string
Password           string
BirthDate          string `sql:"type:date;default:current_time"`
AssignedSex        bool
Show               bool
Sleep              bool
Disabled           bool
}

对于
Sleep
字段,您可以使用指针,该指针也可以是nil:

func UpdateUser(userUpdate user_account) {

db, err := gorm.Open("postgres", "user=postgres password=06maneco dbname=HookTest sslmode=disable")
var user user_account

if err != nil {
    fmt.Println(err)
} else {

    db.Where("id = ?", userUpdate.ID).First(&user)
    if userUpdate.Name != user.Name {
        user.Name = userUpdate.Name
    } else if userUpdate.Password != "" {
        user.Password = userUpdate.Password
    } else if userUpdate.Gender_Identity_id != "" {
        user.Gender_Identity_id = userUpdate.Gender_Identity_id
    } else if userUpdate.Email != "" {
        user.Email = userUpdate.Email
    } else if userUpdate.Show != user.Show {
        user.Show = userUpdate.Show
    }
    db.Save(&user)

}
现在您可以检查它:

type userAccount struct {
    Sleep *bool
}

在操场上查看:.

对于
睡眠
字段,您可以使用指针,指针也可以是零:

func UpdateUser(userUpdate user_account) {

db, err := gorm.Open("postgres", "user=postgres password=06maneco dbname=HookTest sslmode=disable")
var user user_account

if err != nil {
    fmt.Println(err)
} else {

    db.Where("id = ?", userUpdate.ID).First(&user)
    if userUpdate.Name != user.Name {
        user.Name = userUpdate.Name
    } else if userUpdate.Password != "" {
        user.Password = userUpdate.Password
    } else if userUpdate.Gender_Identity_id != "" {
        user.Gender_Identity_id = userUpdate.Gender_Identity_id
    } else if userUpdate.Email != "" {
        user.Email = userUpdate.Email
    } else if userUpdate.Show != user.Show {
        user.Show = userUpdate.Show
    }
    db.Save(&user)

}
现在您可以检查它:

type userAccount struct {
    Sleep *bool
}

在操场上查看:。

这是从哪里来的?你说“如果你发送一个用户帐户对象”-从哪里发送?怎么用?什么格式?
false
false
,您无法区分两者。您确定不能使用
*bool
?或者避免根据结构中bool的值做出决定,或者使用指针并检查它是否为nil。这是一个REST API。用户帐户将由端点处理。例如,我正在努力检测是否通过POST收到了一个“{name:'username”,“show:false}”。Go将其转换为显示为false的用户帐户结构。但是如果我收到一个“{name:'username'}”,user_account结构也会有一个show=false。你们明白了吗?这是从哪里来的?你说“如果你发送一个用户帐户对象”-从哪里发送?怎么用?什么格式?
false
false
,您无法区分两者。您确定不能使用
*bool
?或者避免根据结构中bool的值做出决定,或者使用指针并检查它是否为nil。这是一个REST API。用户帐户将由端点处理。例如,我正在努力检测是否通过POST收到了一个“{name:'username”,“show:false}”。Go将其转换为显示为false的用户帐户结构。但是如果我收到一个“{name:'username'}”,user_account结构也会有一个show=false。你们明白了吗?这是一个好主意,但如果我试图在sleep字段中存储一个值,它将不起作用,因为它不能使用true(type bool)作为类型*boo@LeonardoAndrigo请看游乐场链接,我更新了它,向您展示了设置*bool字段的不同方法,你应该指出一些variable@hsrvthaks它帮助了很多人有一个好主意,但是如果我试图在sleep字段中存储一个值,它将不起作用,因为它说不能使用true(type bool)作为类型*boo@LeonardoAndrigo请看游乐场链接,我更新了它,向您展示了设置*bool字段的不同方法,你应该指出一些variable@hsrvthaks这帮了大忙