Strange Go syntax: what do the parentheses before the function name mean?
Hello everyone! I just started learning Go after Python and JavaScript, and I'm confused by one syntax feature. In code, I often see something like this:
```go
type Rectangle struct {
Width float64
Height float64
}
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
```
Why are there parentheses with `(r Rectangle)` before the function name `Area`? In Python, I would just write `def area(self):`. I understand it has something to do with struct methods, but I can't grasp the logic.
Here's another example that confused me:
```go
func (p *Person) SetName(name string) {
p.Name = name
}
```
Why sometimes they write `(p *Person)` and sometimes `(p Person)`? And what happens if I remove these parentheses altogether? For example:
```go
func Area() float64 {
return 0
}
```
Is this
No answers yet.
Log in to leave an answer