This article analyzes the anonymous attribute characteristics of struct in go language. Share to everyone for your reference. The specific analysis is as follows:
The properties of the struct in the go language can have no name and only a type, and the type is the property name when used. (therefore, there can be only one anonymous property of the same type in a struct)
Copy Code code as follows:
Type Personc struct {
ID int
Country string
}
Anonymous properties
Type Worker struct {
If the worker has an attribute ID, then worker.id represents the worker object's ID
If worker does not have a property ID, then worker.id represents the ID of the personc in the Worker object
ID int
Name string
Int
*personc
}
Func structTest0404 () {
W: = &worker{}
W.id = 201
W.name = "Smith"
W.int = 49
W.personc = &personc{100001, "the"
Fmt. Printf ("name:%s,int:%d\n", W.name, W.int)
Fmt. Printf ("Inner personc,id:%d,country:%s\n",
W.personc.id, W.personc.country)
Fmt. Printf ("worker.id:%d,personc.id:%d\n", W.id, W.personc.id)
/*output:
name:smith,int:49
Inner Personc,id:100001,country:china
worker.id:201,personc.id:100001
*/
}
I hope this article will help you with your go language program.