Go Basic Learning Five interface interface, reflection reflection

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

Go programming Language: Support concurrent, garbage collection of compiled system-level programming language! This paper is mainly based on the "Go Programming Basics" Open source video to learn and record notes.

First, interface interface

1. Basic Concepts

The Go language provides another data type, the interface, which puts the 所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口 .

2. Definition

/* 定义接口 */type interface_name interface {   method_name1 [return_type]   method_name2 [return_type]   method_name3 [return_type]   ...   method_namen [return_type]}/* 定义结构体 */type struct_name struct {   /* variables */}/* 实现接口方法 */func (struct_name_variable struct_name) method_name1() [return_type] {   /* 方法实现 */}...func (struct_name_variable struct_name) method_namen() [return_type] {   /* 方法实现*/}

Example:

package mainimport (    "fmt")type Phone interface {    call()}type NokiaPhone struct {}func (nokiaPhone NokiaPhone) call() {    fmt.Println("I am Nokia, I can call you!")}type IPhone struct {}func (iPhone IPhone) call() {    fmt.Println("I am iPhone, I can call you!")}func main() {    var phone Phone    phone = new(NokiaPhone)    phone.call()    phone = new(IPhone)    phone.call()}

In the example above, we define an interface phone with a method call () inside the interface. Then we define a phone type variable in the main function and assign it to Nokiaphone and iphone, respectively. The call () method is then called and the output is as follows:

I am Nokia, I can call you!I am iPhone, I can call you!

3. Characteristics

    • An interface is a collection of one or more 方法 signatures
    • As long as a type has all the method signatures for that interface, that interface is implemented, without showing which interface is declared, which is called Structural Typing
    • Interfaces are only method declarations, no implementations, no data fields
    • Interfaces can be embedded anonymously in other interfaces, or embedded in a structure
    • When an object is assigned to an interface, a copy occurs, and the interface stores a pointer to the replica that cannot modify the state of the replica or retrieve the pointer
    • The interface is equal to nil only if the interface stores both the type and the object are nil
    • Interface calls do not automatically convert receiver
    • Interface also supports anonymous field methods
    • Interfaces can also be implemented to resemble the polymorphism in OOP
    • An empty interface can be used as a container for any type of data

Example:

package mainimport "fmt"  // 定义一个接口类型type USB interface {    Name() string    Connect()}// 上边的方法可以使用下面的一种嵌入式修改/*type Connecter interface {    Connect()}type USB interface {    Name() string    Connecter}*/// 定义一个结构体type PhoneConnecter struct {  name string}// 定义一个类型为结构体类型的方法func (pc PhoneConnecter) Name() string {    return pc.name}func (pc PhoneConnecter) Connect() {    fmt.Println("Connect:", pc.name)}func Disconnect(usb USB) {    if pc, ok := usb.(PhoneConnecter); ok {        fmt.Println("Disconnect:", pc.name)        return     }    fmt.Println("Unknow device")}func main() {    var a USB    a = PhoneConnecter{"PhoneConnecter"}    a.Connect()    Disconnect(a)}

Print:

➜  myfirstgo go run interface.goConnect: PhoneConnecterDisconnect: PhoneConnecter
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.