What do the parentheses before the function name mean in Go?
Hi everyone! I recently started learning Go and the function declaration syntax is very confusing to me. I'm used to Python where everything is simple: `def func_name(args):`. But in Go I see strange constructs.
For example, I came across this code:
```go
type Rectangle struct {
Width float64
Height float64
}
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
```
What is this `(r Rectangle)` before the function name `Area`? I thought parameters were written in parentheses after the function name, like in other languages. But here they are somehow before the name. Is this some special syntax?
I tried writing just `func Area(r Rectangle) float64` — it works, but then I can't call the method as `rect.Area()`, only as `Area(rect)`. What's the difference? When should I use
No answers yet.
Log in to leave an answer