I come from languages which don't have explicit pointers, so I don't really understand the point of their existence (no pun intended).
The problem is that I have no idea, most of the time, why I am passing a pointer to a function. I do understand that when you pass in a pointer, modifications to the variable are done to the value everywhere, but what's the point? Why not just modify the value and return the result?
For example, http.HandlerFunc
is a function that receives http.ResponseWriter
and *http.Request
as arguments. I have read that interfaces are in fact pointers (is that right?), but what I'm not getting is, why?
Why am I getting a pointer to a writer? I'm not modifying it, I'm merely writing to it. And, why am I getting a pointer to a request? I am doing stuff like request.FormValue()
.
What I'm trying to determine here, through these examples, is the answer to the question, "when do I need to pass in a pointer?"
The way I do it right now is to write my code, try to compile it, fix the errors that say I must pass in a pointer by adding an ampersand and asterisk, until the errors pass. However, I feel this half-understood concept of pointers is going to bite me in the backside some day very soon.