Go-interface interface

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

1. Overview
Interface is a collection of abstract methods (methods that are not specifically implemented/methods that contain only method name parameter return values), somewhat like but different from the Interface in other programming languages.
If all the methods in interface are implemented, that is, the class/object implements the interface

2. Interface type
Format:
Type InterfaceName Interface {  //Method list}
Note:
a. Interface can be implemented by arbitrary objects, and a type/object can also implement multiple interface
b. Method cannot be overloaded, such as Eat () eat (s string) cannot exist simultaneously

3. Interface Value
Variables declared as interface types can store any variable (object) that implements the type of all methods in interface
Note: The value type delivery method of a class automatically generates a corresponding reference type delivery method, which is not true

4. Interface Combination
Embed one Interface1 in the declaration of another Interface2
It acts as a function of including Interface1 in the Interface2, but there are different methods of repetition in the composition
Note:
a. As long as the list of methods in the two interfaces is the same (regardless of order), that is the same interface, which can be assigned to each other
b. The Interface1 method list belongs to a subset of another Interface2 's method list, Interface2 can be assigned to Interface1, and vice versa (because the method is missing), the Interface2 method overrides Interface1 Method with the same name in
C. Interface can be embedded in the package

5. Interface Query
The query can determine whether the object it points to is a type
By querying you can convert a interface1 that is a subset into a interface2 type, that is, Interface1 can invoke a method that is unique to Interface2
Common usage:
  If varName2, OK: = varName1. (Interface2|typename); OK {//    at this time the type of varName2 is changed from Interface1 to Interface2, or varName1 is not a variable of type TypeName  } else {    //cannot convert interface, or VA RNAME1 is not a variable of type TypeName  }
Note:
a. VarName2 Save varName1 Value, varName1 to interface variable, () as type
b. VarName. (type) is used to determine the type, which cannot be used in logic outside of switch, where the type keyword

Sample program:
Package Mainimport ' FMT ' type person struct {name stringage int}func (P person) printmsg () {fmt. Printf ("I am%s, and my age was%d.\n", P.name, P.age)}func (P person) eat (s string) {Fmt. Printf ("%s is eating%s ... \ n", P.name, s)}func (P person) drink (s string) {Fmt. Printf ("%s is drinking%s ... \ n", P.name, s)}type people interface {printmsg () Peopleeat//combination peopledrink//eat ()//cannot appear duplicate Method}/*//with the above equivalent type people interface {printmsg () eat () drink ()}*/type Peopledrink Interface {drink (s string)}type Peopleeat interface {Eat (s string)}type Peopleeatdrink interface {eat (S string) drink (s string)}//above the person class [type] is implemented PEOPL E/peopledrink/peopleeat/peopleeatdrink interface type Foodie struct {name String}func (f Foodie) Eat (s string) {Fmt. Printf ("I am foodie,%s. My favorite food is the%s.\n ", F.name, s)}//foodie class implements the Peopleeat interface type Func main () {//Define a People interface type variable P 1var P1 Peoplep1 = person{"Rain", 23}p1.printmsg ()//i am Rain, and my Age is 23.p1.drink ("Orange juice")//pRint Result:rain is drinking orange juice//the same class can belong to more than one interface, as long as this class implements the method in this interface var p2 PeopleEatp2 = person{"Sun" , 24}p2.eat ("Chaffy dish")//print Result:sun is eating chaffy dish ...//different classes can also implement the same Interfacevar p3 PEOPLEEATP3 = foodie{ "James"}p3.eat ("noodle")//print result:i am Foodie, James. My favorite food is the noodle//interface assignment P3 = P1//p3 method will be overwritten P1 in P3.eat ("noodle")/********************************* *print result *//*rain is eating noodle ... *//************************************///in Terface Query//CONVERT (subset) Peopleeat to people type if P4, OK: = P2. (people); OK {p4.drink ("water")//Call People interface there are no methods in Peopleeat and FMT.            Println (p4)}/************************************//*print result *//*sun is drink water ... *//*{sun 24} *//************************************///query whether P2 is a person type variable if P5, OK: = P2. (person); OK {fmt. Println (P5, "type is a person") p5.drink ("* * *")//can also be invoked at this time PerSon all methods}/************************************//*print result *//*{sun} type is person  *//*sun is drink * * * ... *//************************************/var p6 peopleeat = foodie{"Tom"}if P7, OK: = P6. (people); OK {fmt. Println (P7)} else {fmt. Println ("Error:can not convert")}//result:error:can not convertif P8, OK: = P6. (Foodie); OK {fmt. Println (P8, "type is Foodie")}//result: {Tom} type is Foodie}

Operation Result:
I am Rain, and my age is 23.
Rain is drinking orange juice ...
Sun is eating chaffy dish ...
I am Foodie, James. My favorite food is the noodle.
Rain is eating noodle ...
Sun is drinking water ...
{Sun 24}
{Sun.} type is a person
Sun is drinking * * * ...
Error:can not convert
{Tom} type is Foodie

6. Null interface
Null interface, no method, i.e. interface{}, can represent any type, somewhat similar to the Object class in Java
Can be used as any formal parameter and return type, mixed
Example:
Interface{}var i interface{} = 100var s interface{} = "Hello" fmt. Printf ("i =%d, S =%s\n", I, s) s = ifmt. Printf ("i =%d, S =%d\n", I, s)
Results:
i = +, s = Hello

i = 100, s =


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

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.