Methods
Suppose we have:
type Person struct {
Name string
Age int
}One way to print information is using a normal function.
func Introduce(p Person) {
fmt.Println("Hi, I'm", p.Name)
}Usage:
Introduce(person)Go also allows behavior to be attached directly to a type using methods.
Defining a Method
type Person struct {
Name string
}
func (p Person) Introduce() {
fmt.Println("Hi, I'm", p.Name)
}Usage:
person := Person{
Name: "Alice",
}
person.Introduce()Output:
Hi, I'm AliceReceiver
The part before the method name is called the receiver.
func (p Person) Introduce() {
}Components:
(p Person)-
pis the receiver variable. -
Personis the receiver type.
Conceptually, the method belongs to the type Person.
Methods vs Functions
Function:
func Greeting(p Person) {
}Called as:
Greeting(person)Method:
func (p Person) Greeting() {
}Called as:
person.Greeting()Methods generally make code easier to read because behavior is attached to the data it operates on.
Methods Are Not Inside Structs
Unlike C++ or Java, methods are defined outside the struct.
type Person struct {
Name string
}Later:
func (p Person) Greeting() {
}This keeps data definitions separate from behavior.
Methods Returning Values
Methods can return values just like ordinary functions.
func (p Person) Greeting() string {
return "Hello " + p.Name
}Usage:
fmt.Println(person.Greeting())Output:
Hello AliceReceiver Naming
Receiver names are typically short.
Preferred:
func (p Person)
func (b Book)
func (r Rectangle)Long receiver names are uncommon unless they improve readability.
Value Receivers
Example:
type Counter struct {
Value int
}
func (c Counter) Increment() {
c.Value++
}Usage:
counter := Counter{}
counter.Increment()
fmt.Println(counter.Value)Output:
0Why?
A value receiver receives a copy of the struct.
Changes affect only the copy.
Visualization:
Original
│
├── copy ──► MethodPointer Receivers
To modify the original object:
func (c *Counter) Increment() {
c.Value++
}Usage:
counter := Counter{}
counter.Increment()
fmt.Println(counter.Value)Output:
1A pointer receiver receives the address of the struct rather than a copy.
Visualization:
Original
▲
│
Pointer
│
MethodChanges affect the original object.
Automatic Dereferencing
Even with a pointer receiver:
func (c *Counter) Increment() {
}Go allows:
counter.Increment()instead of
(&counter).Increment()The compiler automatically takes the address when appropriate.
Similarly, when a value receiver is expected but a pointer is available, Go automatically dereferences it.
Choosing Between Value and Pointer Receivers
Use a value receiver when:
-
The method only reads data.
-
The struct is small.
-
Copying is inexpensive.
Example:
func (r Rectangle) Area() intUse a pointer receiver when:
-
The method modifies the struct.
-
The struct is large.
-
Copying would be inefficient.
Example:
func (c *Counter) Increment()Methods Example
type Rectangle struct {
Width int
Height int
}
func (r Rectangle) Area() int {
return r.Width * r.Height
}Usage:
rect := Rectangle{
Width: 5,
Height: 8,
}
fmt.Println(rect.Area())Output:
40Concepts Learned
Method
func (receiver Type) Method() {
}Associates behavior with a type.
Receiver
(p Person)The object on which the method operates.
Value Receiver
func (p Person)Receives a copy.
Cannot modify the original object.
Pointer Receiver
func (p *Person)Receives the original object.
Can modify its fields.
Automatic Dereferencing
Go automatically converts between values and pointers when calling methods.
Comparison with C++
| Go | C++ |
|---|---|
func (p Person) Greet() | Person::Greet() |
| Methods defined outside struct | Methods usually declared inside class |
| Value receiver | Pass-by-value member function |
| Pointer receiver | Member function operating on the original object (this) |
Unlike C++, Go does not place methods inside the struct definition.
Session Summary
Topics covered:
- Methods
- Receivers
- Methods vs functions
- Value receivers
- Pointer receivers
- Automatic dereferencing
- Choosing receiver types
Practical skills gained:
- Attaching behavior to structs
- Deciding when to use value or pointer receivers
- Understanding how Go treats method calls
Key Takeaways
- A method is a function with a receiver.
- Receivers associate behavior with a type.
- Value receivers operate on copies.
- Pointer receivers operate on the original object.
- Go automatically handles many pointer conversions during method calls.
- Structs contain data; methods provide behavior.
Next Topics
- Packages
- Exported vs unexported identifiers
- Project organization
- Imports across packages
- Go Learning - Session 6