The difference between the new method of the go language initialization structure

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Beginner go, when instantiating an object, make a bit dizzy, before learning PHP, need a new one between objects, but in go there are three ways, as follows

A:=new (user) b:=&user{}c:=user{}

As if these three methods can be achieved, what is the reason, the difference?

OK, take a look at the following example

Package Mainimport ("FMT") type user struct {id int ' 1123 '}func main () {A: = &user{} a.id = 111 B: = user{} b.id = 222 c: = new (user) C.id = 333 FMT. Println (A, B, c)}

After running the results are as follows

&{111} {222} &{333}

Now it's basically clear that the data returned by both methods new (user) and &user{} is the same, so what's the difference between these two types of data? What scenarios should we use?

Baidu a bit

&{111} returns a pointer to the instantiated object

{222} returns a type

Detailed further study is required

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.