- Notifications
You must be signed in to change notification settings - Fork1
ovechkin-dm/go-dyno
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
Latest documentation is availablehere
Install latest version of the library using go get command:
go get -u github.com/ovechkin-dm/go-dyno
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.