php editor Baicao will introduce to you the parameters of the Golang decorator function. In Golang, a decorator function is a special function that can be used to wrap other functions and add extra functionality to them. Decorator functions usually have three parameters: the original function, the parameters of the decorator function, and the return value. The original function is the function that needs to be decorated. The parameters of the decorator function can be of any type and can be used to pass additional parameters to the decorator function. The return value is usually a function that replaces the execution of the original function. Through these parameters, we can implement various flexible decorator modes, add different functions to functions, and improve the reusability and scalability of the code.
Question content
I want to use setters on several methods of adminapi, such as update
. To do this, I created a method type that can be matched with other methods.
Should I use an interface instead of func type
?
type adminapi struct { } type toadminctx func(ctx context.context, req interface{}) (interface{}, error) func (a adminapi) adminm2msetter(s toadminctx) toadminctx { return func(ctx context.context, arg interface{}) (interface{}, error) { m2mprincipal, _ := a.getm2mprincipal(ctx) ctxm2m := extlib.setprincipal(ctx, m2mprincipal) return s(ctxm2m, arg) } } func (a adminapi) update(ctx context.context, req *reqtype) (resptype, error) {}
updateWithAdminCtx := a.adminAPI.AdminM2MSetter(s.adminAPI.Update) // ERROR => cannot use s.adminAPI.Update (value of type func(ctx // context.Context, req *ReqType) (RespType, error)) as grpcAdmin.ToGetAdminCtx value in // argument to s.adminAPI.AdminM2MSetter _, err := updateWithAdminCtx(ctx context.Context, req *ReqType)
Solution
I think the error you are encountering is self-explanatory:
a.adminapi.adminm2msetter(s.adminapi.update)
Calling
func (a adminapi) adminm2msetter(s toadminctx) toadminctx {
Pass in s.adminapi.update
as a parameter, the expected type is toadminctx
. Your type definition is:
type toadminctx func(ctx context.context, req interface{}) (interface{}, error)
But the second parameter of your update
function is *reqtype
, and its first return value is the resptype
value, so update
is not toadminctx
. toadminctx
A function type is a function that can be called using a context and literally any type . Your update
function is not guaranteed to work in all cases where the toadminctx
function does.
What you are looking for is a way to "wrap" any function, add some work on the ctx
parameters (maybe set some values), and then pass the call. Prior to go 1.19 we did this by adding some kind of wrapper type like this:
type wrapper struct { updatereqtype *reqtype anothertype *reqtype2 // for some other call you want to wrap }
Change all related functions, such as the update
function to take a wrapper parameter type:
func (a adminapi) update(ctx context.context, req wrapper) (resp, error) { realreq := req.updatereqtype // get the actual request used here }
Response types will be similarly wrapped and/or combined.
Now that go supports generics, they are very useful in this case, let's change the adminm2msetter
function to look like this:
func adminm2msetter[t any, r any](s func(context.context, t) (r, error)) func(context.context, t) (r, error) { return func (ctx context.context, arg t) (r, error) { m2mprincipal, _ := a.getm2mprincipal(ctx) ctxm2m := extlib.setprincipal(ctx, m2mprincipal) return s(ctxm2m, arg) } }
This way, we only need to define this function once, but rely on the compiler to generate a tailor-made function for all types we need. For the update
function, we do the following:
a.adminapi.adminm2msetter[*reqtype, resptype](s.adminapi.update)
Essentially replaces the generic t
and r
types with the specific types used by the update
function. Since I don't really know what function you are trying to wrap this way, I used t any, r any
, but since it seems to me that you are trying to wrap some kind of request handler, So you can create your own constraints:
type Requests interface { *ReqType1 | *ReqType2 | *ReqType3 // and so on } type Responses interface { Resp1 | Resp2 | Resp3 }
Just replace [t any, r any]
with [t requests, r responses]
The above is the detailed content of What parameters do Golang decorator functions have?. For more information, please follow other related articles on the PHP Chinese website!

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
