DEV Community

Jeff Lindsay
Jeff Lindsay

Posted on

A higher-level Go reflection package

Yesterday I did a quick, unscheduled stream to start a Go package that provides a simpler reflection API for Go values. Go has a reflect package that's pretty great, but doing basic things like calling a method or getting a property isn't as trivial as it could be. Plus the nature of reflection means this usually takes a lot of experimentation to get right. At least for me, and I've worked with Go reflection quite a bit.

The idea was to provide an API that looks more like the syscall/js API, which is used for interacting with JavaScript values of WebAssembly hosts. Besides being simpler, this API has methods for getting and setting properties and calling methods. These are some of the most common operations in reflection, but using reflect by itself doesn't just give you these.

So that's all this is. A simple higher level wrapper for the reflect package. This can be used for more easily making JavaScript proxy objects to Go values in WebAssembly or exposing values over RPC, both of which I expect to be doing. But also, I use reflection a lot and this will save a lot of time remembering how to do some of these basic operations.

Anyway, I got the simple version of the interesting methods done, as well as some basic tests. I might work on this more off stream or as needed in projects that use this.

Top comments (0)