Skip to content

Instantly share code, notes, and snippets.

@bengsfort
Created January 12, 2026 11:34
Show Gist options
  • Select an option

  • Save bengsfort/54227dd99e4772421e8883b3d3078b19 to your computer and use it in GitHub Desktop.

Select an option

Save bengsfort/54227dd99e4772421e8883b3d3078b19 to your computer and use it in GitHub Desktop.
Node.js Addon Example in Go
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() {}
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