英文:
How to set bool pointer to true in struct literal?
问题
我有以下函数,它接受一个bool指针。我想知道是否有任何符号可以让我在结构字面值中将is
字段的值设置为true
,而不需要定义一个新的标识符(例如 var x := true; handler{is: &x}
)。
package main
import "fmt"
func main() {
fmt.Println("Hello, playground")
check(handler{is: new(bool)})
}
type handler struct {
is *bool
}
func check(is handler) {}
英文:
I have the function below which accepts a bool pointer. I'm wondering if there is any notation which allows me to set the value of the is
field to true
in the struct literal; basically without to define a new identifier (i.e. var x := true ; handler{is: &x} )
package main
import "fmt"
func main() {
fmt.Println("Hello, playground")
check(handler{is: new(bool) })
}
type handler struct{
is *bool
}
func check(is handler){}
答案1
得分: 73
你可以这样做,但这并不是最优解:
h := handler{is: &[]bool{true}[0]}
fmt.Println(*h.is) // 输出 true
基本上,它创建了一个包含一个值为 true
的 bool
的切片,索引其第一个元素并取其地址。没有创建新变量,但是有很多样板代码(并且直到指向其第一个元素的地址存在,支持数组将一直存在于内存中)。
一个更好的解决方案是编写一个辅助函数:
func newTrue() *bool {
b := true
return &b
}
然后使用它:
h := handler{is: newTrue()}
fmt.Println(*h.is) // 输出 true
你也可以使用一行匿名函数来实现:
h := handler{is: func() *bool { b := true; return &b }()}
fmt.Println(*h.is) // 输出 true
或者一个变体:
h := handler{is: func(b bool) *bool { return &b }(true)}
要查看所有选项,请查看我的其他答案:https://stackoverflow.com/questions/30716354/how-do-i-do-a-literal-int64-in-go/30716481#30716481
英文:
You can do that but it's not optimal:
h := handler{is: &[]bool{true}[0]}
fmt.Println(*h.is) // Prints true
Basically it creates a slice with one bool
of value true
, indexes its first element and takes its address. No new variable is created, but there is a lot of boilerplate (and backing array will remain in memory until the address to its first element exists).
A better solution would be to write a helper function:
func newTrue() *bool {
b := true
return &b
}
And using it:
h := handler{is: newTrue()}
fmt.Println(*h.is) // Prints true
You can also do it with a one-liner anonymous function:
h := handler{is: func() *bool { b := true; return &b }()}
fmt.Println(*h.is) // Prints true
Or a variant:
h := handler{is: func(b bool) *bool { return &b }(true)}
To see all your options, check out my other answer: https://stackoverflow.com/questions/30716354/how-do-i-do-a-literal-int64-in-go/30716481#30716481
答案2
得分: 6
这是将bool
转换为*bool
的最简单方法。
func BoolPointer(b bool) *bool {
return &b
}
h := handler{is: BoolPointer(true)}
英文:
This simplest way is to write a short function to turn a bool
into a *bool
.
func BoolPointer(b bool) *bool {
return &b
}
h := handler{is: BoolPointer(true)}
答案3
得分: 5
没有。
除了new
返回的零值之外,没有定义指向原始类型的指针的语法。对于数值类型和字符串也是如此。
你要么需要事先创建一个值来获取其地址,要么使用零值创建指针,然后在之后分配一个新值。
英文:
No.
There is no syntax to define a pointer to a primitive type, other than the zero value returned by new
. The same goes for numeric types, and strings.
You either need to create a value before hand to take the address of, or you create the pointer with a zero value, and assign a new value after the fact.
答案4
得分: 4
我使用了一个类似于@icza的函数,但更加方便(对我来说)。
我在我的utils
包中创建了一个名为BoolAddr
的函数。
package utils
func BoolAddr(b bool) *bool {
boolVar := b
return &boolVar
}
对我来说,这样使用更加简单。
package main
import "example.com/example/utils"
...
type Example struct {
isActive *bool
}
ex := Example{
isActive: utils.BoolAddr(true),
}
...
英文:
I used a function similar to @icza but in a way more convenient (for me)
I created a BoolAddr function in my utils package
package utils
func BoolAddr(b bool) *bool {
boolVar := b
return &boolVar
}
For me it's easier to use
package main
import "example.com/example/utils"
...
type Example struct {
isActive *bool
}
ex := Expample {
isActive: utils.BoolAddr(true)
}
...
答案5
得分: -2
指针在Go语言或其他任何语言中有帮助的原因之一是它们帮助我们进行“按引用传递”。因此,如果我们通过引用传递任何东西,我们可以“更改”该东西。一个接受bool指针的函数可以在函数返回后有效地更改bool的值。这正是我们不希望常量发生的情况,即它们的值不应该改变。因此,这个限制是有道理的。
除了上面提到的icza
的技巧之外,我想在这里补充一点。通常我们使用指向bool的指针而不是直接使用bool,以便有效地使用指针的nil值,否则指针只能是true或false。如果是这种情况,那么您可能希望在函数中直接使用可选的bool标志,而不是使用bool指针或甚至像您的示例中展示的包装单个bool指针的结构体,甚至可以省去使用结构体的完全要求。当然,如果结构体出于其他原因是必需的,您仍然可以使用icza
上面提到的任何技巧。
顺便说一下,您也可以直接复制bool值并使用其地址,如下所示:
const check = true
chk := check
fmt.Println(&chk) // 将给出chk的地址
chk = false
fmt.Println(chk) // 将打印false
fmt.Println(check) // 将打印true
英文:
One of the reasons why pointers are helpful in go or any language for that matter, is they help us to "pass by reference". So if we pass anything by reference we can then "change" that thing. A function which takes a pointer to bool, can change the bool's value effectively even after the function returns. This is the very thing we do not want with constants, ie. their values should not change. Hence this restriction makes sense.
Apart from the tricks mentioned by icza
above, would want to add a point here. Mostly we use pointers to bools rather than bools directly in order to use the nil value of pointers effectively, which otherwise have to be either true or false. If that IS the case, then you might want to use optional bool flags directly in the functions, rather than have pointers to bool or even a struct wrapping the single bool pointer as shown in your example, doing away with the complete requirement of a struct even.. Now, of course if the struct is reqd for any other reason, you can very well use any of the tricks by icza
above.
Btw, you can directly have a copy of the bool value for using the adress of as below as well.
const check = true
chk := check
fmt.Println(&chk) // will give you the address of chk
chk = false
fmt.Println(chk) // will print false
fmt.Println(check) // will print true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论