[Go language] learn go--interfaces from Docker source

Source: Internet
Author: User

Interface Definition:
Interface {    Method1 (param_list) return_type    Method2 (param_list) return_type    ...}

Note:

1. Cannot include variables in interface

2. A type does not explicitly define the implementation of an interface, as long as it contains all the methods defined in the interface, this type implements the interface by default

3. Multiple types can implement the same interface

4. While a type implements an interface, it can also define its own method

5. A single type can implement multiple interfaces

Nested interface
Interface {    bool bool interface {    Lock ()          Interface {    ReadWrite    Lock    Close ()}

So file will contain all the methods in ReadWrite and lock

Determine the type of interface
V: = VarI. ( T)//VarI must is an interface variable.

Instance

Package Lemontype ShaperInterface{area () Float32}type Squarestruct{Side float32}func (sq*Square) Area () float32 {returnSq. Side *Sq. Side}//Use in main func.func showinterfaceusage () {sq1:=New(Lemon. Square) sq1. Side=5    varareaintf Lemon. Shaper areaintf=sq1ifV, OK: = areaintf. (*Lemon. Square); OK {fmt. Printf ("The square has an area :%f\n", V.area ())}}

Note: The type passed in the definition of the method is a pointer, and a pointer is also passed when judging.

    Switch areaintf. (type) {    case *Lemon. Square:        FMT. Printf ("TheSquare area is%f\n", Areaintf.area ())    Default  :        fmt. Printf ("Thedefault type")    }

can also be directly through the method interface. (type) to obtain the type of the interface

Determines whether the type implements the interface

... Cond

[Go language] learn go--interfaces from Docker source

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.