I'm new to golang. Just trying to understand the difference between the two methods declared for Calc type. The first function sum is declared as (c *Calc) Sum and the other one without * as (c Calc) Minus. What's the difference between the two and recommended way of writing this?
I see the difference comes in how we call the function under main. The point * type method requires new(Calc) and the other one can be called directly by doing Calc{}.Sum.
Some behavioural explanation will help.
func main() {
Calc{}.Minus(2, 2)
c :=new(Calc)
c.Sum(3, 2)
}
type Calc struct{
Result int
}
func (c *Calc) Sum(a int, b int) {
c.Result = a + b
}
func (c Calc) Minus(a int, b int) {
c.Result = a-b
}