The interface in Go

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

For Golang interface, tangled two days, today there is a kind of enlightened feeling, it is necessary to write something.

Tangled interface, it is clear that the interface, method, structure of the relationship between several and specific purposes. Can be simple from the definition of the three, the interface is a method set, the struct is a class, usually save the property, the method is equivalent to the function of the class, but there are two types of recipients (objects), one is a value, one is a pointer, the pointer can change the property values in the structure. In go, as long as the structure of the method implementation (including) the interface of all the methods, it can be said that the structure of the implementation of this interface. After the definition is clear, you can see what the interface is all about, and then give the two code.

package main

    import "fmt"

    type act interface {                                       //定义一个act接口,包含一个write方法

      write()

  }

  type xiaoming struct {                                    //xiaoming结构体

  }

  type xiaofang struct {                                     //xiaofang结构体

   }

  func (xm *xiaoming) write() {                         //xiaoming结构体的方法write,接收者为指针类型。即xiaoming实现了act接口

    fmt.Println("xiaoming write")

  }

  func (xf *xiaofang) write() {                          //同上,xiaofang实现了act接口

    fmt.Println("xiaofang write")

  }

  func main() {

    var w act                                     

    xm := xiaoming{}

    xf := xiaofang{}

    w = &xm                                          //实例化接口,由于xm是指针类型的接收者,必须加&

    w.write()

    w = &xf                                           //同上

    w.write()

  }

The output is: Xiaoming Write
Xiaofang Write

This example also illustrates the object-oriented polymorphic nature of the go language. An interface is actually a method of invoking a struct as an intermediate.

A second example:

The interface has two methods, area and perimeter, and two get methods are written, respectively, to calculate areas and perimeter (this step is actually useless). Then, the rectangular and circular structures are defined separately, and the Shape interface is implemented separately. In the Func main () {}, the Get method and the call interface member method are used to calculate the perimeter and area, as you can see, The function of an interface is to call the member method of the struct as an "intermediate" there is no need to redefine the method inside the interface. in short, the use of interfaces can be very flexible, and the specific implementation of the understanding of decoupling, if later there are other implementation classes, only need to implement the interface can be, and do not have to change the use of the time of the code.

    

Type Rect struct {width, height float64} func (R *rect) area () float64 {return r.width * R.height} func (R *rect) Peri Meter () float64 {return (r.height + r.width) * 2}//------------round------------//type circle struct {radius float64} F UNC (c *circle) area () float64 {return math. Pi * C.radius * C.radius} func (c *circle) perimeter () float64 {return 2 * math. Pi * C.radius}
Author: Iron-core-Wood-ji
Links: http://www.imooc.com/article/4579
Source: MU-Class Network

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.