-
Notifications
You must be signed in to change notification settings - Fork 40
FFI Go
Andy Arvanitis edited this page Sep 5, 2019
·
2 revisions
-
Foreign values are resolved dynamically/lazily (and "cached" thereafter), so you don't need to define all of them right away. If needed and missing during runtime, a
panic
will be thrown. -
To implement foreign values to be exported:
- TBD
-
See the standard library implementations for more examples
Some basic conventions used in the Go FFI code (not absolutely required, but please try to follow them)
- Run your code through
gofmt
- Use provided type alias
Any
instead ofinterface{}
- Use provided type alias
Dict
instead ofmap[string]Any
(intended for when you're dealing with ps records) - Use provided type
[]Any
for arrays - For input parameters used as-is (i.e. when no type assertion from
Any
is necessary), simply use a name without a trailing underscore.- For example:
exports["foo"] = func(bar Any) Any { return bar }
- For example:
- For input parameters needing a type assertion from
Any
to a concrete type, use a name with a trailing underscore, then declare a type-inferred variable, with underscore removed, assigning the type assertion operation to it. This also allows the possiblity of a faster type assertion – getting and ignoring a failure code instead of apanic
. When debugging, you might want to restore thepanic
possiblity until you're sure it's not an issue.- For example:
exports["foo"] = func(bar_ Any) Any { bar, _ := bar_.(int) // the ", _" gets and ignores the error flag ... }
- And when debugging:
exports["foo"] = func(bar_ Any) Any { bar := bar_.(int) // remove ", _" to get friendly type assertion panics ... }
- For example: