Go:Receiver
lastmod 2026-10-27created 2025-11-193 edits
About
There are two ways to define a method.
Pointer receiver
You can read it as in C. Like Call-By-Pointer, it copies the "pointer" of this.
Value receiver
You can read it as in C. Like Call-By-Value, it copies the "value" of this.
Example
package main
import "fmt"
type Person struct {
Name string
Age int
}
// Pointer receiver - 원본 수정 가능
func (p * Person) SetAge(age int) {
p.Age = age
}
// Value receiver - 원본 수정 불가
func (p Person) SetAge(age int) {
p.Age = age
}
// Value receiver - 원본 수정 불가
// Works on a copy, doesn't modify original
func (p Person) GetAge() int {
return p.Age
}
func main() {
person := Person{Name: "Alice", Age: 20}
person.SetAge(25) // 원본이 25로 변경됨 (Changes original to 25)
fmt.Println(person.Age) // 출력: 25
// 만약 SetAge가 값 리시버였다면?
// If SetAge was a value receiver:
// person.Age는 여전히 20 (would still be 20)
}
See also
Pages that link here