4
I have a struct called Calculator, with two properties: version and author. To be able to instantiate this struct already initiating these methods, since Golang has no constructors, the various tips I found on the net indicate to create a Newcalculator() method and then use it to instantiate the calculator. The method would look like this:
func NewCalculadora() *Calculadora {
return &Calculadora{autor:"Paulo Luvisoto",versao:1.0}
}
And all tips indicate the use of pointer, as above. But I saw that so also works:
func NewCalculadora() Calculadora {
return Calculadora{autor:"Paulo Luvisoto",versao:1.0}
}
My question is: what is the problem of using this second way, without using pointer?
Golang will "convert" internally if you use
func (c *Calculadora) ...
, then use&
would be preferable. That is, as far as I know.– Inkeliz