understand golang interface by example

tosoth · · 2979 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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 main

import "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.


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:tosoth

查看原文:understand golang interface by example

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2979 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传