This is a creation in Article, where the information may have evolved or changed.
Type person struct { Name string address Address}type Address struct {number string Street string< C6/>city string state string, Zip string}func (P *person) talk () { fmt. Println ("Hi, my Name is", P.name)}func (P *address) location () { //fmt. Println ("I ' m at", P.address.number, P.address.street, p.address.city, P.address.state, P.address.zip) FMT. Println ("I ' m at", P.number, P.street, p.city, P.state, P.zip)}func main () { p: = person{ Name: "Steve", Ad dress:address{number : "$", Street: "Main", City : "Gotham", State : "NY", Zip : "01313", }, } p.talk () //P.location () wrong p.address.location ()}
Above is a "use-a" code snippet, but note " address is still a different object, except that it exists in person. "
Type person struct { Name string address}type Address struct {number string Street string City string State string Zip string}func (P *person) talk () { fmt. Println ("Hi, my Name is", P.name)}func (P *address) location () { //fmt. Println ("I ' m at", P.address.number, P.address.street, p.address.city, P.address.state, P.address.zip) FMT. Println ("I ' m at", P.number, P.street, p.city, P.state, P.zip)}func main () { p: = person{ Name: "Steve", Ad dress:address{number : "$", Street: "Main", City : "Gotham", State : "NY", Zip : "01313", }, } p.talk () p.location () p.address.location ()}
Above is a "is-a" code snippet, but note " is-a relationships are implemented by introducing anonymous domains (person). The person is an anonymous domain (anonymous field) for citizen, and the anonymous field gives only the object type, not the name of the type. With an anonymous domain, citizen can access all the attributes (domains) and methods in the person. ".
Part of the code and text to go is the object-oriented language? 》。