I recently started to learn Golang for a work project. I come from a 'c' background and have some confusion about the technicalities of Pointer Receivers in functions. Am I to assume receiver are generalized and don't care if the object is actually a pointer or a literal, it will translate it to whatever the receiver type is.
I know my question wording may be confusing so here is some code for example:
func (v *clients) makePtr() {
(*v).slice = make([]client, 0)
return
}
func (v clients) makeLit() []client {
return make([]client, 0)
}
func main() {
clsPtr := &clients{} // pointer
clsLit := clients{} // literal
clsLit.makePtr() // literal with pointer reciever
clsLit.makeLit() // literal with pointer reciever
clsPtr.makePtr() // pointer with literal reciever
clsPtr.makeLit() // pointer with pointer reciever
}
I expected the the functions with pointer receivers to only work on pointers and vice versa. from this am I just suppose to assume receivers are a blanket statement and don't care if its a pointer, literal?
EDIT: Apologizes guys, I may not have been to clear with my question, I understand that a pointer receiver is used to modify a object but I'm more confused as to the syntax, why does a pointer receiver work for both a literal and a pointer, since it receives a pointer shouldn't it only work on a pointer?