https://github.com/akshayganeshen/napi-go
Go library for building Node.js Native Addons using Node-API
https://github.com/akshayganeshen/napi-go
addon go golang n-api napi nodejs
Last synced: about 2 months ago
JSON representation
Go library for building Node.js Native Addons using Node-API
- Host: GitHub
- URL: https://github.com/akshayganeshen/napi-go
- Owner: akshayganeshen
- License: mit
- Created: 2022-06-13T23:21:57.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-06-27T00:14:30.000Z (over 3 years ago)
- Last Synced: 2025-09-18T14:00:47.220Z (6 months ago)
- Topics: addon, go, golang, n-api, napi, nodejs
- Language: Go
- Homepage:
- Size: 40 KB
- Stars: 45
- Watchers: 3
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# napi-go
A Go library for building Node.js Native Addons using Node-API.
## Usage
Use `go get` to install the library:
```sh
go get -u github.com/akshayganeshen/napi-go
```
Then use the library to define handlers:
```go
package handlers
import "github.com/akshayganeshen/napi-go"
func MyHandler(env napi.Env, info napi.CallbackInfo) napi.Value {
return nil
}
```
Next, create a `main.go` that registers all module exports:
```go
package main
import "github.com/akshayganeshen/napi-go/entry"
func init() {
entry.Export("myHandler", MyHandler)
}
func main() {}
```
Finally, build the Node.js addon using `go build`:
```sh
go build -buildmode=c-shared -o "example.node" .
```
The output `.node` file can now be imported via `require`:
```js
const example = require("./example.node");
example.myHandler();
```
### JS Helpers
In addition to the Node-API exposed via package `napi`, the `napi-go/js`
package provides functions similar to the `syscall/js` standard library.
```go
package main
import (
"github.com/akshayganeshen/napi-go/entry"
"github.com/akshayganeshen/napi-go/js"
)
func init() {
entry.Export("myCallback", js.AsCallback(MyCallback))
}
func MyCallback(env js.Env, this js.Value, args []js.Value) any {
return map[string]any{
"message": "hello world",
"args": args,
}
}
func main() {}
```
## Examples
Check out the example addons in [`docs/examples`](docs/examples).