Understand Golang interface by example

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

Recently in Huawei, we is trying to the change some kubernetes source code in order to optimize its performance. Golang is new for me, so I just want to find some a-to-find the-the-in. And I got these word online:

To know Golang, check, Things:interface and channel

So here we are, interface.

Some BAIDU, I got this

OH, Nooooooo. Can not understand a word.

So I think why not some example to check for a newbee like me.

All the example are seen here

https://gobyexample.com/

And I got this:

OK, since I have already has an Golang env on my MAC

Time to start

_interfaces_ is named collections of method//signatures.package mainimport "fmt" import "math"//here ' s a basic interf  Ace for geometric Shapes.type geometry interface {area () float64 Perim () float64}//for our example we ' ll implement This interface on//' rect ' and ' circle ' types.type rect struct {width, height float64}type circle struct {radius float64}//to implement a interface in Go, we just need to//implement all the methods in the interface. Here we//implement ' geometry ' in ' rect ' S.func (R rect) area () float64 {return r.width * R.height}func (R rect) Perim ( ) Float64 {return 2*r.width + 2*r.height}//The implementation for ' Circle ' S.func (c circle) area () float64 {retur N Math. Pi * C.radius * C.radius}func (c circle) Perim () float64 {return 2 * math. Pi * c.radius}//If A variable have an interface type and then we can call//methods that is in the named interface. Here's a//generic ' measure ' function taking advantage of this//to work on any ' geomEtry '. Func measure (g geometry) {fmt. Println (g) fmt. Println (G.area ()) fmt.  Println (G.perim ())}func main () {r: = Rect{width:3, height:4} c: = circle{radius:5}//' Circle ' and ' rect ' struct types both//implement the ' Geometry ' interface so we can use//instances of//these structs as Argume    NTS to ' measure '. Measure (r) measure (c)}

Easy code to Calc area of a surface. But in advance we don't know if the surface is a rect or circle. Although to it's all called measure no matter how the surface looks like, we measure in diffrent ways by how the surface Looks like.

Confused? Let us do it more clear. What I got are so we had three concept here.

1. The surface (Rect, circle or STH)

2. Measure the action (we can measure the height, measure the area)

3. The the-the-measure-a-rect, how-to-measure a circle

1 is the object, and 2 is what we can do to an unknown object.

and 3, make connect between 1 and 2.

In 1 ' s view, OK they can does sth for me.

In 2 ' s view, the WOW finally I know who I can does this to.

and 3 just make connections. Generally these connections is written on 1 ' s side.

So the connections has been set, how can I use them?

First, we implement a surface like circle with a radius call C (1). Since the implementation with 3

Then we call

C.area ()

System would check table in 2, and 2 says OK, I know area, if you need to tell me whose area it was, and 1 said it is a CIR CLE, 2 said OK, then here's how to check area of circle.

We have the answer.

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.