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: