Home > Backend Development > Golang > How Can I Extend Existing Types in Go When Direct Extension Is Not Supported?

How Can I Extend Existing Types in Go When Direct Extension Is Not Supported?

DDD
Release: 2024-12-13 18:24:14
Original
333 people have browsed it

How Can I Extend Existing Types in Go When Direct Extension Is Not Supported?

Extending Existing Types in Go

In Go, extending existing types defined in external packages is not directly supported. However, there are workarounds to achieve a similar result.

Let's consider the example provided in the question:

package util

import(
    "net/http"
    "github.com/0xor1/gorillaseed/src/server/lib/mux"
)

func (r *mux.Route) Subroute(tpl string, h http.Handler) *mux.Route{
    return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
}

func (r *mux.Router) Subroute(tpl string, h http.Handler) *mux.Route{
    return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
}
Copy after login

When attempting to compile this code, one encounters the error: "Cannot define new methods on non-local type mux.Router". This is because Go does not allow direct extension of types from external packages.

Workaround 1: Define a Wrapper Type

One workaround is to define a wrapper struct that embeds the original type and provides the desired methods. For example:

type MyRoute struct {
    *mux.Route
}

func (r *MyRoute) Subroute(tpl string, h http.Handler) *mux.Route {
    return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
}

type MyRouter struct {
    *mux.Router
}

func (r *MyRouter) Subroute(tpl string, h http.Handler) *mux.Route {
    return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
}
Copy after login

Workaround 2: Type Assertion

Another option is to use type assertion to convert the original type to an interface{} and then to a custom type that provides the desired methods. For example:

func (r interface{}) Subroute(tpl string, h http.Handler) *mux.Route {
    switch v := r.(type) {
    case *mux.Route:
        return v.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
    case *mux.Router:
        return v.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h)
    default:
        panic("unreachable")
    }
}
Copy after login

Note: These workarounds may introduce additional complexity and should be used carefully. If possible, it is best to avoid modifying external types and instead define your own types that wrap or extend the functionality you need.

The above is the detailed content of How Can I Extend Existing Types in Go When Direct Extension Is Not Supported?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template