Why is there a "new" in Go?
Posted
by
dystroy
on Programmers
See other posts from Programmers
or by dystroy
Published on 2013-09-04T07:31:19Z
Indexed on
2013/11/05
10:11 UTC
Read the original article
Hit count: 314
go
|instantiation
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 ?
© Programmers or respective owner