Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dynamic proxy for golang

License

NotificationsYou must be signed in to change notification settings

ovechkin-dm/go-dyno

Repository files navigation

Build StatusCodecovGo Report CardDocumentationReleaseLicense

Dynamic proxy for golang

Go-dyno is a lightweight and flexible library for Go, inspired by Java's dynamic proxy mechanism. It enables dynamic creation of proxy implementations for interfaces at runtime, allowing developers to intercept and customize method calls programmatically. This makes it a powerful tool for building dynamic behaviors and enhancing flexibility in Go applications.

Documentation

Latest documentation is availablehere

Quick start

Install latest version of the library using go get command:

go get -u github.com/ovechkin-dm/go-dyno

Example

This is an example of proxy that prints method name before any method invocation of the original instance.

package mainimport ("fmt""reflect""github.com/ovechkin-dm/go-dyno/pkg/dyno")typeGreeterinterface {Greet()stringSayHello(namestring)string}typeSimpleGreeterstruct {}func (g*SimpleGreeter)Greet()string {return"Hello!"}func (g*SimpleGreeter)SayHello(namestring)string {returnfmt.Sprintf("Hello, %s!",name)}typeProxyHandler[Tany]struct {ImplT}func (p*ProxyHandler[T])Handle(m reflect.Method,values []reflect.Value) []reflect.Value {fmt.Println("Method called:",m.Name)returnreflect.ValueOf(p.Impl).MethodByName(m.Name).Call(values)}funcmain() {greeter:=&SimpleGreeter{}proxyHandler:=&ProxyHandler[Greeter]{Impl:greeter}dynamicGreeter,err:= dyno.Dynamic[Greeter](proxyHandler.Handle)iferr!=nil {fmt.Println("Error creating dynamic greeter:",err)return}fmt.Println(dynamicGreeter.Greet())fmt.Println(dynamicGreeter.SayHello("World"))}

About

Dynamic proxy for golang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp