加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

一段Golang代码引发的思考

发布时间:2020-12-16 18:28:34 所属栏目:大数据 来源:网络整理
导读:1.前言 Golang有一些异于其他语言的特性,如果对这些特性不了解,看一些代码的时候经常就会感到莫名其妙。最近在看Kubernetes的一段代码时,由于没有深刻领会Golang的接口机制导致一脑袋雾水,当真正理解了之后,不得不佩服Golang的灵活性,堪称神一般的存在

1.前言

Golang有一些异于其他语言的特性,如果对这些特性不了解,看一些代码的时候经常就会感到莫名其妙。最近在看Kubernetes的一段代码时,由于没有深刻领会Golang的接口机制导致一脑袋雾水,当真正理解了之后,不得不佩服Golang的灵活性,堪称神一般的存在。

2.分析

这段代码的部分片段如下,详细内容在Kubernets github:https://github.com/kubernetes/kubernetes/blob/master/pkg/admission/chain.go

1)chan.go

// chainAdmissionHandler is an instance of admission.Interface that performs admission control using a chain of admission handlers
type chainAdmissionHandler []Interface

// NewFromPlugins returns an admission.Interface that will enforce admission control decisions of all
// the given plugins.
func NewFromPlugins(client clientset.Interface,pluginNames []string,configFilePath string) Interface {
	plugins := []Interface{}
	for _,pluginName := range pluginNames {
		plugin := InitPlugin(pluginName,client,configFilePath)
		if plugin != nil {
			plugins = append(plugins,plugin)
		}
	}
	return chainAdmissionHandler(plugins)
}

// NewChainHandler creates a new chain handler from an array of handlers. Used for testing.
func NewChainHandler(handlers ...Interface) Interface {
	return chainAdmissionHandler(handlers)
}

// Admit performs an admission control check using a chain of handlers,and returns immediately on first error
func (admissionHandler chainAdmissionHandler) Admit(a Attributes) error {
	for _,handler := range admissionHandler {
		if !handler.Handles(a.GetOperation()) {
			continue
		}
		err := handler.Admit(a)
		if err != nil {
			return err
		}
	}
	return nil
}

// Handles will return true if any of the handlers handles the given operation
func (admissionHandler chainAdmissionHandler) Handles(operation Operation) bool {
	for _,handler := range admissionHandler {
		if handler.Handles(operation) {
			return true
		}
	}
	return false
}
这段代码里面包含了一个类型的定义:type chainAdmissionHandler []Interface,两个函数Admit和Handlers都是 chainAdmissionHandler这个类型的函数,也就是Interface数组的接口的实现。注意:Admin和Handlers是Interface的数组的接口实现这点很重要。两个函数NewFromPlugins,NewChainHandler返回值是Interface,但是我们发现chainAdmissionHandler(plugins)得到的命名是Interface的数组。Interface的数组和Interface肯定是不同的类型啊,编译发现居然不报错。

下面我们看看Interface接口的定义:

// Interface is an abstract,pluggable interface for Admission Control decisions.
type Interface interface {
	// Admit makes an admission decision based on the request attributes
	Admit(a Attributes) (err error)

	// Handles returns true if this admission controller can handle the given operation
	// where operation can be one of CREATE,UPDATE,DELETE,or CONNECT
	Handles(operation Operation) bool
}
原来Interface也定义的是Admit和Handlers方法,也就是说chainAdmissionHandler实现了Interface接口。所以,返回 chainAdmissionHandler并不报错。

3.总结

Golang的接口实现非常灵活,只要一个接口体或者类型实现了接口里面所有的方案就算实现了接口。所以跟Java不通,Golang没有implements关键字。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读