Junior — Middle
How to determine which part of the structure the method belongs to?
sobes.tech AI
Answer from AI
In Go, a method is defined as a function with a receiver, which indicates the structure (or other type) it belongs to. The receiver is specified in parentheses before the method name.
For example:
type User struct {
Name string
}
func (u User) Greet() string {
return "Hello, " + u.Name
}
Here, the Greet method belongs to the User struct because it has a receiver (u User). Therefore, to determine which part of the structure the method belongs to, you need to look at the receiver type in the method declaration.