Is there a better way to allocate the contents of this array, such as automatically calling the NewThing()
constructor instead of manually constructing each element?
package main
import "sync"
type Thing struct {
lock *sync.RWMutex
data chan int
}
func NewThing() *Thing {
return &Thing{ lock: new(sync.RWMutex), data: make(chan int) }
}
func main() {
n := 10
things := make([]*Thing, n)
for i := 10; i < n; i++ {
things[i] = NewThing()
}
}
I realize i'm allocating an array of pointers, my other attempts were unsuccessful and data was not an initialized channel. This is just a contrived example.
Thanks!