Preface
Today, in coding, I saw a very classic interface usage as follows. So I looked up the relevant information and found that this writing method is an interface function. This article explains this in detail.
// A Getter loads data for a key. type Getter interface { Get(key string) ([]byte, error) } // A GetterFunc implements Getter with a function. type GetterFunc func(key string) ([]byte, error) // Get implements Getter interface function func (f GetterFunc) Get(key string) ([]byte, error) { return f(key) }
How to use interfaces in GO?
In the above routine, an interface is first defined, and then a function type is defined to implement this interface. So how do you use the interface in the GO language?
In the GO language, an interface is a type that defines a combination of a set of methods, but there is no specific code implementation. The interface definition example is as follows:
type MyInterface interface { Method1() string Method2(int) int }
In the GO language, the implementation of interfaces is implicit. The interface implementation should be bound to a type, and the interface is implicitly implemented by implementing the type method. The implementation example is as follows:
type MyType struct { // type fields } func (t *MyType) Method1() string { return "Hello, world!" } func (t *MyType) Method2(n int) int { return n * n }
After implementing the interface, we can pass the interface as parameters into a certain function, so that different data structures of the interface can be passed into the function as interface:
func MyFunction(i MyInterface) { (i.Method1()) (i.Method2(8)) }
When calling this function, you can first declare the data structure that implements this interface, and then call the function:
func main() { t := &MyType{} MyFunction(t) }
After the call, the result will be generated:
Hello, world!
64
What are the benefits of using function types to implement interfaces?
The above is the structure used to implicitly implement the interface, and you can also customize the function type to implicitly implement the interface. In this way, anonymous functions or ordinary functions (both type conversion is required) can be directly passed into the function as interface parameters. The interface and implementation are as follows:
type MyInterface interface { Method1() string } type MyInterfaceFunc func()string func (f MyInterfaceFunc) Method1()string { return f() }
Define a function with an interface as a parameter:
func MyFunction(i MyInterfaceFunc){ (i.Method1()) }
Use normal functions to call:
func Dog()string{ return "dog dog !" } func main() { MyFunction(MyInterfaceFunc(Dog)) }
Calling using anonymous functions:
func main() { MyFunction(MyInterfaceFunc(func() string { return "hello!" })) }
As you can see, the final output is correct:
dog dog !
hello!
In general, the code is greatly increased. If there is no interface function, if you want to implement a new function, you need to declare a new type (such as a structure), then implicitly implement the interface, and then pass it into the MyFunction function. With the interface function, you only need to implement the core logic, and then convert the function to the expected type and pass it in directly.
GO source code example
The Handler and HandlerFunc of net/http are typical examples of interface functions. The definition of Handler is as follows:
type Handler interface { ServeHTTP(ResponseWriter, *Request) } type HandlerFunc func(ResponseWriter, *Request) func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) }
You can use to map classic mapping paths and processing functions:
func Handle(pattern string, handler Handler)
Observing this function, we can find that it is very similar to the example above. The handler here is the interface type, and then it is implemented based on HandlerFunc. Then we can use it as follows:
func home(w , r *) { () _, _ = ([]byte("hello, index page")) } func main() { ("/home", (home)) _ = ("localhost:8000", nil) }
After running, it will listen to localhost:8000 and run the home function. Here, it is very convenient to convert the home type to pass it in as an interface type.
We can also use the form of anonymous functions:
func main() { ("/home", (func(writer , request *) { () ([]byte("hello word!")) })) _ = ("localhost:8000", nil) }
The same effect can be achieved.
This is the article about the specific use of interfaces and interface-type functions in GO language. For more related GO language interfaces and interface-type functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!