前言
从java转go以来一直在寻找动态代理的实现方案,几经波折,终于找到了位牛人写的框架monkey,简单喵了下其源码,貌似是通过替换底层函数字节码来达到动态替换效果,后续再仔细拜读。这里我利用monkey框架实现了一个简单的AOP框架
monkey项目地址:https://github.com/bouk/monkey
aop码云地址:https://gitee.com/sqxwww/aop
AOP框架
package aop
import (
"bou.ke/monkey"
"fmt"
"reflect"
"regexp"
)
//连接点
type JoinPoint struct {
Receiver interface{}
Method reflect.Method
Params []reflect.Value
Result []reflect.Value
}
func NewJoinPoint(receiver interface{}, params []reflect.Value, method reflect.Method) *JoinPoint {
point := &JoinPoint{
Receiver: receiver,
Params: params,
Method: method,
}
fn := method.Func
fnType := fn.Type()
nout := fnType.NumOut()
point.Result = make([]reflect.Value,