Is there a way to avoid instantiating a variable first and then assigning it to a struct which is expecting some property to be a reference value, like in the example below?
type Example struct {
Active *bool `json:"age,omitempty"`
}
func main() {
active := false
e := Example {
Active: &active,
}
}
A simple function like the one below(Ref
) would remove the need to first create a variable and then use it in the struct, but I am not sure if I won't eventually shoot myself in the foot in the future because of it.
type Example struct {
Active *bool `json:"age,omitempty"`
}
func main() {
e := Example {
Active: Ref(true),
}
}
func Ref[T any](val T) *T {
return &val
}