Interface Summary for Go

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

First, what is the interface

    The
      • interface type is an abstract type that describes a collection of methods.
      • Interface convention: The method defined in the interface type is the contract, and if a specific type implements all of these methods, the type satisfies the contract of that interface, or It is an instance of this interface type (which implements the interface).
      • replaceable (LSP Richter replacement): The types that meet the same interface conventions can be substituted with each other. For example, if a method's formal parameter is defined as an interface type, it can receive an argument for any type that satisfies the interface convention.
      • interface inline: interface types can be combined by combining existing interfaces To define the
      • io. The writer interface provides an abstraction of all types written to bytes, including file types, memory buffers, network links, HTTP clients, compression tools, hashes, and so on; io.reader can represent any type of bytes that can be read, io. Closer can be any value that can be closed, such as a file or a network link. There are also fmt. Stringer interface, etc.
      • interface type name usually ends with "er"

Second, what is the interface value

    • Interface value: The value of the interface variable, consisting of two parts, a specific type and a value of that type. They are called dynamic types and dynamic values of interfaces
    • 0 value of the interface value: The dynamic type, and the corresponding dynamic value, value are nil, such as Var w io. Writer
    • Null interface value: null interface value when and only if the dynamic type of the interface and the corresponding dynamic value of value are nil, it is equal to nil at this time
    • The assignment of the interface variable and the calling procedure:
        • such as W = OS. Stdout, This assignment procedure invokes an implicit conversion of a specific type to an interface type, and explicitly uses IO. Writer (OS. Stdout) is equivalent. The dynamic type of the interface value W is set to *os. StdOut the type descriptor of the pointer, its dynamic value holds the OS. Copy of stdout
        • Call a containing *os. The Write method for the interface value of the file type pointer, W.write ([]byte ("" Hello ")), making (*os. File). The Write method is called
    • An interface value can hold any large dynamic value, no matter how large the dynamic value, the interface value can always tolerate it
    • The comparability of the interface values:
        • Always remember that only comparison of dynamic types is an interface value of comparable types.
        • If the dynamic types of the interface values are comparable, then they can be compared using = = and! =: two interface values are equal only if they are nil values or their dynamic types are the same and the dynamic values are equal according to the = = operation of the dynamic type.
        • If the interface values are comparable, then they can be used in the map's key or as the operand of the switch statement
        • Non-interface types are either safe, comparable types (such as base types and pointers) that are either completely non-comparable types (such as slices, mapping types, and functions), but we must be aware of potential panic when comparing interface values or aggregation types that contain interface values. The same risk exists in the use of interfaces as a key to a map or as a switch operand.
    • Note: An interface that contains a nil pointer is not a nil interface (an empty interface), and an panic error occurs when calling an interface method. That is, the dynamic type of an interface value, a. = nil, but the dynamic value = = Nil, at this point the interface value W! = nil. (This occurs when a variable of a non-interface type with a value of nil is converted to an interface type)
    • Tip: When using an interface, declare a variable of an interface type directly, and then assign a value to it, and then use it to compare it to nil to determine if it is an empty interface
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.