Created
January 12, 2026 11:34
-
-
Save bengsfort/54227dd99e4772421e8883b3d3078b19 to your computer and use it in GitHub Desktop.
Node.js Addon Example in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| // Build with `go build -buildmode=c-shared -o addon.node` | |
| /* | |
| #cgo CFLAGS: -I./vendor | |
| #cgo darwin LDFLAGS: -Wl,-undefined,dynamic_lookup | |
| #cgo linux LDFLAGS: -Wl,--unresolved-symbols=ignore-all | |
| #cgo windows LDFLAGS: -Wl,--allow-shlib-undefined | |
| #include <node_api.h> | |
| */ | |
| import "C" | |
| //export napi_register_module_v1 | |
| func napi_register_module_v1(env C.napi_env, exports C.napi_value) C.napi_value { | |
| var world_str C.napi_value | |
| C.napi_create_string_utf8(env, C.CString("World"), C.size_t(5), &world_str) | |
| C.napi_set_named_property(env, exports, C.CString("hello"), world_str) | |
| return exports | |
| } | |
| func main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const addon = require('addon.node'); | |
| console.log(addon.hello); // prints "World" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment