This is a creation in
Article, where the information may have evolved or changed.
1. Preface
Golang has some features that are different from other languages, and if you don't understand them, you'll often find it confusing to see some code. Recently, while looking at Kubernetes's piece of code, the lack of a deep understanding of the Golang interface mechanism led to a head fog, when the real understanding, have to admire Golang flexibility, called the existence of God.
2. Analysis
Some fragments of this code are as follows, detailed in 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 handlerstype Chainadmissionhandler []interface//NEWF Romplugins returns an admission. Interface that would enforce admission control decisions of all//the given Plugins.func newfromplugins (client clientset. Interface, Pluginnames []string, ConfigFilePath String) Interface {plugins: = []interface{}for _, Pluginname: = Range Plug innames {plugin: = Initplugin (pluginname, client, configfilepath) if plugin! = Nil {plugins = append (plugins, plugin)}}retu RN 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 Errorfunc (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 would return true if any of the handlers Handles the given Operati Onfunc (Admissionhandler Chainadmissionhandler) Handles (Operation operation) bool {For _, Handler: = Range Admissionhandl Er {if handler. Handles (operation) {return True}}return false}This code contains the definition of a type: type Chainadmissionhandler []interface, two functions admit and handlers are
Chainadmissionhandler This type of function, which is the implementation of the interface of the interface array. Note: It is important that the admin and handlers are interfaces for interface arrays. The two function Newfromplugins,newchainhandler return value is interface, but we find that Chainadmissionhandler (plugins) Gets the name of the interface array. Interface array and interface are certainly different types Ah, compile found actually not error.
Let's look at the definition of the interface interface:
Interface is a abstract, pluggable Interface for admission Control decisions.type Interface Interface {//admit makes An admission decision based on the request Attributesadmit (a Attributes) (err Error)//Handles Returns True if this Admiss Ion Controller can handle the given operation//where operation can be one of CREATE, UPDATE, DELETE, or Connecthandles (op eration operation) BOOL}
Originally interface also defined is the admit and handlers method, that is to say Chainadmissionhandler implements the interface interface. So, return
Chainadmissionhandler not error.
3. Summary
The Golang interface is very flexible, as long as an interface body or type implements the interface in which all the schemes are implemented. So with Java does not pass, Golang no implements keyword.