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}