Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of Go language Interface assignment

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Go language interface assignment example introduction", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Go language interface assignment examples" bar!

Assign an object instance to an interface

First, let's take a look at assigning an object instance of a specified type to an interface, which requires that the class corresponding to the object implement all the methods required by the interface, which is natural, otherwise it cannot be counted as implementing the interface. For example, we defined an Integer type before.

Type Integer int func (an Integer) Equal (I Integer) bool {return a = = I} func (an Integer) LessThan (I Integer) bool {return a

< i} func (a Integer) MoreThan(i Integer) bool { return a >

I} func (a * Integer) Increase (I Integer) {* a = * a + I} func (a * Integer) Decrease (I Integer) {* a = * a-I}

Accordingly, we define an interface IntNumber:

Type IntNumber interface {Equal (I Integer) bool LessThan (I Integer) bool MoreThan (I Integer) bool Increase (I Integer) Decrease (I Integer)}

Install the conventions of the Go language, and the Integer type implements the IntNumber interface. Then we can assign the object instance corresponding to the Integer type to the IntNumber interface as follows:

Var an Integer = 1var b IntNumber = & a

Notice that in the above assignment statement, we assign the pointer application of the object instance a to the interface variable, why should we do so? Because the Go language is based on non-pointer member methods like the following:

Func (an Integer) Equal (I Integer) bool

Automatically generate a new pointer member method corresponding to it:

Func (a * Integer) Equal (I Integer) bool {return (* a) .Equal (I)}

In this way, the type * Integer has all the methods declared in the IntNumber interface, and the Integer type does not contain the pointer methods Increase and Decrease (which we have described earlier), so strictly speaking, only the * Integer type implements the IntNumber interface. If we hastily assign a value reference to b, an error will be reported at compile time

Cannot use a (type Integer) as type IntNumber in assignment: Integer does not implement IntNumber (Decrease method has pointer receiver)

Obviously, if the method implemented in the Integer class is not a pointer method, the value of the object instance is passed to the interface variable when the interface assignment is made, otherwise the pointer variable needs to be passed. To verify this, we can create a new interface, IntNumber2:

Type IntNumber2 interface {Equal (I Integer) bool LessThan (I Integer) bool MoreThan (I Integer) bool}

Then assign the value reference of object instance a to the IntNumber interface variable:

Var an Integer = 1varb1IntNumber=&avar b2 IntNumber2 = a

Then the above two interface assignment statements can be compiled and passed.

Assign an interface to an interface

Next, let's look at assigning one interface to another: in the Go language, as long as two interfaces have the same list of methods (regardless of order), they are equivalent and can be assigned to each other.

Let's write the corresponding sample code, which is the first interface Number1, which is located in the oop1 package:

Package oop1 type Number1 interface {Equal (I int) bool LessThan (I int) bool MoreThan (I int) bool}

This is the second interface, Number2, in the oop2 package:

Package oop2 type Number2 interface {Equal (I int) bool MoreThan (I int) bool LessThan (I int) bool}

Here we define two interfaces, one called oop1.Number1 and the other called oop2.Number2, both of which define five identical methods, but in different order. In the Go language, there is virtually no difference between the two interfaces because:

Any class that implements the oop1.Number1 interface also implements oop2.Number2;. Any object instance that implements the oop1.Number1 interface can be assigned to oop2.Number2, and vice versa; using the oop1.Number1 interface anywhere is no different from using oop2.Number2.

Next, we define a class Number that implements these two interfaces:

Type Number int; func (n Number) Equal (I int) bool {return int (n) = = I;} func (n Number) LessThan (I int) bool {return int (n)

< i;} func (n Number) MoreThan(i int) bool { return int(n) >

I;}

So the following assignment code is legal and will be compiled through:

Var num1 Number = 1bot var num2 oop1.Number1 = num1var num3 oop2.Number2 = num2

In addition, interface assignment does not require that the two interfaces are exactly equivalent (the method is exactly the same). If the method list of interface An is a subset of the method list of interface B, interface B can assign a value to interface A. For example, suppose the Number2 interface is defined as follows:

Type Number2 interface {Equal (I int) bool MoreThan (I int) bool LessThan (I int) bool Add (I int)}

For the Number class to continue to implement these two interfaces, you need to add an Add method implementation to the Number class definition:

Func (n * Number) Add (I int) {* n = * n + Number (I);}

Next, rewrite the above API assignment statement as follows:

Var num1 oop1.Number = 1bot var num2 oop2.Number2 = & num1;var num3 oop1.Number1 = num2

But not the other way around:

Var num1 oop1.Number = 1tervar num2 oop1.Number1 = & num1;var num3 oop2.Number2 = num2; / / the compilation error occurred in this section

Because there is no Add method in the Number1 interface, this is quite similar to the fact that the PHP/Java subclass instance can be assigned directly to the parent class, while the parent class instance cannot be assigned directly to the subclass.

At this point, I believe that everyone on the "Go language interface assignment example introduction" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report