Initialization method of struct in golang

Source: Internet
Author: User
In the Go language, all uninitialized variables are initialized to zero values of this type. for example, if the zero value of the bool type is false and the zero value of the int type is 0, the zero value of the string type is a null string.

Customize a struct

1234 Type Rect struct {x, y float64 width, height float64}

Initialization method:

1234 Rect1: = new (Rect) rect2: = & Rect {} rect3: = & Rect {100,200, 100} rect4 :=& Rect {width: 200, height}

In the Go language, all uninitialized variables are initialized to zero values of this type. for example, if the zero value of the bool type is false and the zero value of the int type is 0, the zero value of the string type is a null string.
There is no constructor concept in the Go language. object creation is usually done by a global creation function. NewXXX is used as a command to represent "constructor ":

123 Func NewRect (x, y, width, ehgith float64) {return & Rect {x, y, width, height }}

This is all natural. developers do not need to analyze how many things happen after new is used. in The Go language, everything that will happen can be seen directly.

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.