I'm having trouble understanding the *
and &
symbols in Go. I understand the concept that pointer receivers allow us to modify the value that a receiver points to:
type Vertex struct {
X, Y float64
}
// This adjust the value of v.X and v.Y
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
But I also see functions returning the a pointer receiver, for example the net/http library.
func NewRequest(method, url string, body io.Reader) (*Request, error)
In this scenario, why is there a star in front of the Request
being returned?
Additionally, I often see &
used when assigning variables
import net/http
client := &http.Client{}
Why not just client := http.Client{}
?