今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Struct embedding
- Go takes the old adage of preferred composition over inheritance to heart, and doesn't support the notion of inheritance at all. What it does though, is provide a seamless way to compose certain types, mainly STRs and interfaces, through what's called embedding. Let's see this in action with STRs. We start out here with a person type, already defined for us along with a method that returns a full name. We'll define a custom type of our own called author. We'll jump to line 17, (Typing) and define the author- type. Since an author is also a person with a first and last name, we'll use go embedding and simply specify the person type as one of the fields for author. We can name this field like any other, or we can leave just a type, and reference it accordingly. Our author type, will have a pen name, so we'll add that as an additional field. And it will be of type string. Down in main, where we already have a…