Go 在结构中引用用于赋值的布尔值
如何更改*IsEnabled=true的值 这些都不起作用:Go 在结构中引用用于赋值的布尔值,go,Go,如何更改*IsEnabled=true的值 这些都不起作用: type MyStruct struct { IsEnabled *bool } 您可以通过将true存储在内存位置,然后访问它来实现这一点,如下所示: *(MyStruct.IsEnabled) = true *MyStruct.IsEnabled = true MyStruct.*IsEnabled = true 从: 布尔型、数值型和字符串型的命名实例为 预先声明。复合类型数组、结构、指针、函数、, 接口、切片、贴
type MyStruct struct {
IsEnabled *bool
}
您可以通过将true存储在内存位置,然后访问它来实现这一点,如下所示:
*(MyStruct.IsEnabled) = true
*MyStruct.IsEnabled = true
MyStruct.*IsEnabled = true
从:
布尔型、数值型和字符串型的命名实例为
预先声明。复合类型数组、结构、指针、函数、,
接口、切片、贴图和通道类型可以使用类型
文字
因为布尔值是预先声明的,所以不能通过复合文字来创建它们(它们不是复合类型)。类型bool
有两个const
值true
和false
。这排除了以以下方式创建文本布尔值:b:=&bool{true}
或类似方式
应该注意的是,将*bool设置为false
要容易得多,因为new()
会将bool初始化为该值。因此:
type MyStruct struct {
IsEnabled *bool
}
func main() {
fmt.Println("Hello, playground")
t := true // Save "true" in memory
m := MyStruct{&t} // Reference the location of "true"
fmt.Println(*m.IsEnabled) // Prints: true
}
我的回答有用吗?它解决了你的问题吗?如果没有,您能否对问题进行澄清,以便我们能够更好地解决任何遗留问题?
m.IsEnabled = new(bool)
fmt.Println(*m.IsEnabled) // False