The struct of Golang

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

1. Used to define complex data structures

2.struct can contain multiple fields (attributes)

The 3.struct type can define methods, notes, and function distinctions.

4.struct type is a value type

5.struct types can be nested

6.Go language has no class type, only struct type

Package Mainimport"FMT"//declaring struct bodiesType Studentstruct{Namestring Ageintscoreint}func Main () {//struct field access, using "point" access    varStu Student Stu. Name="Yuchao"Stu. Age= -Stu. Score= AboutFMT. Printf ("name=%s age=%d scrore=%d", Stu. Name, Stu. Age, Stu. Score)}

Define three forms of a struct:

Package Mainimport"FMT"//declaring struct bodies//The field name is the same as the function name, with the first letter capitalized and accessible outside the packageType Studentstruct{Namestring Ageintscore Float32}func Main () {//struct field access, using "point" access    varStu Student//var stu *student = new (Student)//var stu *student = &student{}Stu. Name ="Yuchao"Stu. Age= -Stu. Score= About    //FMT. Println (Stu)//default structure {Yuchao 99.1}//FMT. Printf ("name=%s age=%d scrore=%d", Stu. Name, Stu. Age, Stu. Score)    varSTU1 *student = &student{Age: -, Name:"Chao",    }    varSTU3 =student{Age: -, Name:"Hua",} fmt. Println (STU1) fmt. Println (STU3. Name) fmt. Printf ("name:%p\n", &Stu. Name) fmt. Printf ("age:%p\n", &Stu. Age) Fmt. Printf ("score:%p\n", &Stu. Score)}
Define structure Body

var Stu Student var New (Student) var stu *student = &student{}

The memory layout of the struct, all fields in the struct are contiguous in memory, and the layout is as follows:

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.