A method is basically a function that you attach to a type
Define a person object would have name, age and more. The keywords “type” and “struct” used to define a person object.
type Person struct{
name string
age int
}
a function with a type (Person) as a receiver before the function name, Go developer label it a “method”.
func (p Person) GetName() string{
return p.name
}
The part between the “func” keyword and the function name, GetName(), is known as the receiver of the method. This method has arguments but returns a string value.
Declare a variable p with value of type Person
var p = Person{
name: "Jason",
age: 29,
}
Invoke the GetName
method of value p
p.GetName()