How can functions be used to implement interfaces in Go?

Mary-Kate Olsen
Release: 2024-11-06 12:53:02
Original
376 people have browsed it

How can functions be used to implement interfaces in Go?

Functions Implementing Interfaces in Go

In Go, interfaces define a set of methods that a type must implement to be considered a member of that interface. This allows for polymorphic behavior, as types implementing the same interface can be used interchangeably in code.

Example 1: Custom Type Implementing Interface

Consider the following interface for an HTTP handler:

type Handler interface {
    ServeHTTP(*Conn, *Request)
}
Copy after login

The following type, Counter, implements this interface by providing a custom implementation of ServeHTTP:

type Counter int

func (ctr *Counter) ServeHTTP(c *Conn, req *Request) {
    fmt.Fprintf(c, "counter = %d\n", ctr)
    ctr++
}
Copy after login

Example 2: Converting Function to Handler

The following function, notFound, does not implement the Handler interface directly:

func notFound(c *Conn, req *Request) {
    c.SetHeader("Content-Type", "text/plain;", "charset=utf-8")
    c.WriteHeader(StatusNotFound)
    c.WriteString("404 page not found\n")
}
Copy after login

To make this function compatible with the Handler interface, Go provides a way to convert a function to a type with a specific method. In this case, the HandlerFunc type is defined:

type HandlerFunc func(*Conn, *Request)
Copy after login

A method, ServeHTTP, is added to this type:

func (f HandlerFunc) ServeHTTP(c *Conn, req *Request) {
    f(c, req)
}
Copy after login

The notFound function can now be converted to an instance of HandlerFunc and assigned to a variable:

var Handle404 = HandlerFunc(notFound)
Copy after login

This allows Handle404 to be used as a Handler, even though the notFound function itself does not directly implement the Handler interface.

Explanation

In summary, Go allows functions to be converted to types with specific methods, addressing situations where a function does not directly fulfill an interface's requirements. By creating a type instance out of the function and defining a method on that type, the function can be accommodated by the interface, enabling polymorphic behavior in the program.

The above is the detailed content of How can functions be used to implement interfaces in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!