Why is there a "new" in Go?
- by dystroy
I'm still puzzled as why we have new in Go.
When you want to instantiate a struct, you do
t := Thing{}
and, obviously, you can get a pointer to a new instance by doing
t := &Thing{}
But there's also this possibility :
t := new(Thing)
This last one seems a little alien to me. &Thing{} is as clear and concise as new(Thing) and it uses only constructs you often use elsewhere. It's also more extensible as you might change it to &Thing{3} or &Thing{Feets:7}.
In my opinion, having a supplementary keyword is costly, it makes the language more complex and adds to what you must know. And it might mask to newcomers what's behind instantiating a struct.
It also makes one more reserved word.
So what's the reasoning behind new ? Is it something useful ? Should we use it ?