Skip to content

visualfc/funcval

Repository files navigation

funcval

golang runtime funcval extract

Go1.14 Go1.15 Go1.16 Go1.17 Go1.18

extract closure env variable

func Get(fn interface{}) (fv *FuncVal, makefunc int)

package main

import (
	"unsafe"

	"github.com/visualfc/funcval"
)

func main() {
	a := 1
	b := "hello"
	c := 10
	fn := func() {
		println(a, b, c)
	}
	c = 20
	fv, _ := funcval.Get(fn)
	type closure struct {
		funcval.FuncVal
		a int
		b string
		c *int
	}
	env := (*closure)(unsafe.Pointer(fv))
	println(env.a, env.b, *env.c)
}

About

golang runtime funcval extract

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages