Example of a method set inheritance rule for Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The author of this article: Bole online-V7. No reprint without the permission of the author!
Welcome to Bó Lè Online columnist. This article shows the inheritance rules for methods set in the Golang mechanism through specific examples.

0x00 rules for simple method

First, we express examples of the basic operating structure of the body.

Go <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">type Cat struct{Name string Color string}</textarea>
1234 type Cat struct {Name string Co Lor string}

We declare a Cat's type, on the basis of which we declare a meow method:

Go <textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">func (c Cat) Meow () {fmt. Println ("Name:", C.name, "Color:", C.color)}</textarea>
123 func (C Cat) Meow() {    FMT.Println("Name:", C.Name, "Color:", C.Color)}

In the above code, we declare a method whose receiver is cat (different from cat's pointer), at which point, whether we call method by the Cat type variable or the type of *cat, go will compile normally and output normally, such as:

, the A and B variables in the main function, one is the cat type, and the other is the cat pointer type, and they all have the Meow method.

So what if the function receiver we're declaring is *cat?

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">func (c *cat) Meow () {fmt. Println ("Name:", C.name, "Color:", C.color)}</textarea>
123 func (c *Cat) Meow() {    FMT.Println("Name:", C.Name, "Color:", C.Color)}

In this case, the method is also inherited:

Above are the inheritance rules for the simple set of methods on Cat and *cat:

In normal cases, the set of methods on the type T and *t are inherited from each other.

Rules for methods set in the 0X01 interface

Next we declare an interface to do the comparison:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">type Meower interface{Meow ()}func Greet (Meower meower) {meower. Meow ()}</textarea>
12345678 type meower Interface {Meow ()} func Greet(meower meower){ meower. Meow ()}

First we implement the Meower interface on the cat type:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">func (c Cat) Meow () {fmt. Println ("Name:", C.name, "Color:", C.color)}</textarea>
123 func (C Cat) Meow(){    FMT.Println("Name:", C.Name, "Color:", C.Color)}

The compilation is no problem at this point.

That is, if we give the cat type a way to implement the Meow on the Meower interface, both the cat and *cat can become interface calls.

So if we implement the interface mode, the receiver chosen is *cat?

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">func (c *cat) Meow () {fmt. Println ("Name:", C.name, "Color:", C.color)}</textarea>
123 func (c *Cat) Meow(){    FMT.Println("Name:", C.Name, "Color:", C.Color)}

Compilation does not pass, such as:

If you implement the Meow method to the pointer, there is a type error on line 26th, suggesting that the receiver of the Meow method is a pointer type, stating that the method cannot inherit at this time.

So the rules here are:

method in the interface, for the normal type T:
T's methods set does not inherit the method that contains the *T implementation unless t implements the corresponding method by itself.
However, *t inherits the method set of T.

0X02 rules for methods set in embedded types

We discussed both of the above scenarios, so what happens if cat is an embedded type (Embedded Types)?

So on top of that, let's declare a type to do the experiment:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">type Blackcat struct{Cat age int}</textarea>
1234 type blackcat struct { Cat age >int }

At this point we are directly able to invoke the cat implementation of the interface method via Blackcat,

, we did not implement an interface for Blackcat, only the cat interface, but blackcat embedded in the cat's internal type, but also through the interface call function greet to call to Meow method.

At the same time we modify the main function, found that the Blackcat methods set contains Meow (), such as:

At this time

In the type of the embedded type, the outer type will also add these methods to its own methods set when the outer type itself is implemented by an methods that has not been implemented.

So what happens if the external type Blackcat itself implements the Meow () function (and also implements the interface), both internally and externally?

Here we give Blackcat implementation Meow () method to try:

<textarea wrap="soft" class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 13px !important; line-height: 15px !important;">func (c blackcat) Meow () {fmt. Println ("Blackname:", C.cat.name, "Age:", C.age)}</textarea>
123 func (C Blackcat) Meow(){    FMT.Println("Blackname:", C.Cat.Name, "Age:", C. Age)}

First, there is no problem with compiling:

Let's take a look at a. Operating results for Cat.meow () and A.meow ():

Discover that Blackcat is using its own method of implementation, or that it covers the Meow () method of the internal cat.

The same is true when you change a direct call to use an interface:

Two times the result is the same.

0X03 Conclusion

Figuring out the method set inheritance in Golang is useful for us to write non-verbose code, whereas in Golang, pointers are handled more flexibly than in C + +.

0x04 Reference

Methods, Interfaces and Embedded Types in Go

Support me to write more good articles, thank you!

reward authors

Support me to write more good articles, thank you!

Choose a payment method

1 likes 1 Collection Reviews

About the Author: v7

Weibo: @_v7__ Home Page • my article · 17 ·    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.