I am making a memory pool in Go.
I am doing so because implicit converting int to interface{} will trigger a memory allocation.I want to avoid the allocation.
I want to allocate several types of pointers in one pool.
The pool is something like this.
type creator struct{
buf []interface{}
}
func (cr *creator) Create()*interface{}{
if len(cr.buf)==0{
cr.buf=make([]interface{},256)
}
current:=&cr.buf[0]
cr.buf=cr.buf[1:]
return current
}
func (cr *creator)CreateInt()*int{
pointer:=cr.Create()
return (*int)(unsafe.Pointer(pointer)) // <-- right or wrong?
}
Is the code valid?