I am learning Go, and I have a question based on the following code:
package main
import (
"fmt"
)
type Vector struct {
x, y, z int
}
func VectorFactory(x,y,z int) *Vector {
return &Vector{x, y, z}
}
func main() {
vect := VectorFactory(1, 2, 3)
fmt.Printf("%d
", (vect.x * vect.y * vect.z))
}
Here I've defined a type Vector
with x
, y
, and z
, and I've defined function VectorFactory
which declares a pointer to a Vector
and returns that pointer. I use this function to create a new Vector
named vect
.
- Is this bad code? Should I be using the
new
keyword rather than building aFactory
? - Do I need to
delete
theVector
after using it, like in C++? If so, how?
Thanks. I'm still waiting for my Go book to be delivered.