Golang Method
Go has both functions and methods. In Go, a method is a function that is declared with a receiver. A receiver is a value or a pointer of a named or struct type. All the methods for a given type belong to the type’s method set.
Let’s declare a struct type and a method for that type:
type User struct {
Name string
Email string
}
func (u User) Notify() error
First we declare a struct type named User and then we declare a method named Notify with a receiver that accepts a value of type User. To call the Notify method we need a value or pointer of type User:
// Value of type User can be used to call the method
// with a value receiver.
bill := User{"Bill", "bill@email.com"}
bill.Notify()
// Pointer of type User can also be used to call a method
// with a value receiver.
jill := &User{"Jill", "jill@email.com"}
jill.Notify()
in the case where we are using a pointer, Go adjusts and dereferences the pointer so the call can be made. Be aware that when the receiver is not a pointer, the method is operating against a copy of the receiver value.
If you are unsure about when to use a value or a pointer for the receiver, always use pointer. The Go wiki has a great set of rules that you can follow.