understand golang interface by example

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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

To know Golang, check two 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 is seen here

https://gobyexample.com/

And I got this:

OK, since I have already have an golang env on my MAC

Time to start to roll

// _Interfaces_ are named collections of method// signatures.package mainimport "fmt"import "math"// Here's a basic interface 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 an interface in Go, we just need to// implement all the methods in the interface. Here we// implement `geometry` on `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 {    return math.Pi * c.radius * c.radius}func (c circle) perim() float64 {    return 2 * math.Pi * c.radius}// If a variable has an interface type, then we can call// methods that are 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}    // The `circle` and `rect` struct types both    // implement the `geometry` interface so we can use    // instances of    // these structs as arguments to `measure`.    measure(r)    measure(c)}

Easy code to calc area of a surface. But in advance we do not know if the surface is rect or circle. Although to it is all called measure no matter how the surface looks like, we measure in diffrent ways by how the surface looks like.

Confused? Let us make it more clear. What I got is so we have three concept here.

1. the surface (rect, circle or sth)

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

3. the way (how to measure a rect, how to measure a circle)

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

And 3, make connect between 1 and 2.

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

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

And 3 just make connections. Generally these connections are written on 1's side.

So the connections have been set, how we can 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 will check table in 2, and 2 says ok, I know area, but you need to tell me whose area it is, and 1 said it is a circle, 2 said ok, then here is how to check area of circle.

We have the answer.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.