注: 转载请注明出处, 原文链接。
概述
在这篇博客中,我将详细介绍 xgo 的实现细节。
如果你不知道,xgo 项目位于 https://github.com/xhd2015/xgo。
它的作用很简单,就是在每个 Go 函数的开头添加拦截器,从而引入了所谓的 Trap
概念,然后在此基础上引入了其他功能,比如 Mock
、Patch
和 Trace
。
什么是 Trap?
Trap 是插入到函数体开头的一段代码。以一个名为 greet
的函数为例:
func greet(s string) string {
return "hello " + s
}
经过xgo的处理后,编译器看到的代码将变为:
import "runtime"
func greet(s string) (r0 string){
stop, post <