Golang reflect package hack tools
- Go 1.21 ~ 1.27
- Go (gc)
- LLGo
- macOS
- Linux
- Windows
- WebAssembly
- ABI0 stack-based ABI
- ABIInternal register-based Go calling convention proposal
- Go1.21+: amd64 arm64 ppc64/ppc64le riscv64
- Go1.23+: amd64 arm64 ppc64/ppc64le riscv64 loong64
- reflectx.CanSet
- reflectx.Field
- reflectx.FieldByIndex
- reflectx.FieldByName
- reflectx.FieldByNameFunc
- reflectx.StructOf
- reflectx.NamedTypeOf
- reflectx.SetUnderlying
- reflectx.SetTypeName
-
reflectx.Method
-
reflectx.MakeMethod
-
reflectx.NewMethodSet
-
reflectx.SetMethodSet
-
reflectx.StructToMethodSet
- reflectx.InterfaceOf
- reflectx.NamedInterfaceOf
- reflectx.NewInterfaceType
- reflectx.SetInterfaceType
- reflectx.NewContext
Default gc path: each installed method needs a unique ifn stub.
- allocs
import _ "github.com/goplus/reflectx/icall/icall[N]"
- install icall_gen
go install github.com/goplus/reflectx/cmd/icall_gen@latest
icall_gen -o icall1024.go -pkg main -size 1024
Alternative to icall: methods share makeFuncStub instead of an icall
stub table. reflectx stores an untagged *makeFuncImpl in addReflectOff
and sets tflagIfaceFuncval (1<<6). A patched runtime tags itab.Fun
(makeFuncImpl*|1) so interface calls unwrap.
Needs a patched Go 1.25.x, 1.26.x, or 1.27.x and an explicit tag.
Supported GOARCH: wasm, arm64, amd64, 386.
- install iface_patch
go install github.com/goplus/reflectx/cmd/iface_patch@latest
iface_patch /path/to/go # 1.25.x, 1.26.x, or 1.27.x
cd /path/to/go/src && ./make.bash
export GOROOT=/path/to/go
export PATH="$GOROOT/bin:$PATH"
go test -tags goplus.ifacefuncval .
# wasip1: GOOS=wasip1 GOARCH=wasm go test -exec wasmtime -tags goplus.ifacefuncval .Without the tag, a patched compiler matches official gc. An unpatched compiler still accepts the tag; interface method calls then trap.
You can export GOFLAGS='-tags=goplus.ifacefuncval', or prefix a command:
GOFLAGS='-tags=goplus.ifacefuncval' go build (also go run / go test).
See cmd/iface_patch/README.md.
go build -tags linknamefix -ldflags="-checklinkname=0"