Golang Structural Notes

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

First, the concept

A struct is an aggregated data type that is an entity that is aggregated by 0 or more values of any type. Each value is called a member of the struct.

II. structure Declaration and use

//declaring struct bodiesType Employeestruct{IDintNamestring}func Main () {//struct instantiationEMP: = Employee{id:1, Name:"Frod"}    //Anonymous field instantiationEMP2: = employee{1,"Frod2"} fmt. PRINTLN (EMP) fmt. Println (EMP2)}

Three, the aggregation structure

Golang does not inherit the concept of the class aggregation is respected

Type Addressstruct{provincestring CitystringDetailstring}type Employeestruct{IDintNamestringAddress Address}func Main () {emp:=employee{ID:1, Name:"Frod", address:address{Province:"Zhejiang province", City:"Hangzhou City", Detail:"Yuhang Cang Qian Zhen",},} FMT. PRINTLN (EMP)}

Iv. Anonymous structural bodies

Func Main () {    emp:struct  {        ID   int        string     } {        ID   :1001        ,"frod" ,    }    Fmt. PRINTLN (EMP)}

V. Structural methods

 type Employee struct   {ID  int   Name  string  }func Main () {emp:  = Employee{1 ,  "  frod   "  this  *employee) getName () string   { return  this  . Name}  

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.