gives me "invalid type for composite literal: bool" - is there any way
to create a pointer to a boolean without using new()?
--
Scott Lawrence
$ cat t.go
package main
func main() {
var t bool
z:=&t
*z = true
}
$ 6g t.go
$
> dest := &bool{}
>
> gives me "invalid type for composite literal: bool" - is there any way
> to create a pointer to a boolean without using new()?
No.
-rob
Yes.
-rob
Not sure why you're declaring the bool first, it seems unnecessary:
t := true
z := &t
If anything, I would allocate the pointer first, then initialize it in a block:
var z *bool; { t := true; z = &t }
This is essentially equivalent to what you'd expect from:
z := &bool{ true }
But, I'm really not sure why you'd want to avoid:
z := new(bool) // *z is false by default
*z = true
--
Scott Lawrence
What gave you that impression? new() is there for a reason.
You can't allocate memory for any of the standard types without new()
also new() is far clearer in stating your intention.
dest := &sometype{} // says, get me a pointer to this thing(which will
be on the heap if it escapes or on the heap if it doesn't)
dest := new(sometype) //says, allocate me some space on the heap
because I expect it to escape.
- jessta
--
=====================
http://jessta.id.au