nodejs-golang v0.0.5
nodejs-golang
Module for running Golang scripts using wasm in Node.js child processes
How It Works
- During module installation
npm i nodejs-golang
Go programming language will be downloaded and installed - In the root of the projects will be created default
go_modules
directory In
go_modules
directory will be createdmain.go
file. Example:package main import ( "fmt" ) func main() { fmt.Print("Hello, go 1.16.2") }
Default
main.wasm
file will be compiled (also can be found ingo_modules
directory)- You need to put you code into
main.go
file To rewrite
main.wasm
with actual info run:node ./node_modules/nodejs-golang/build.js
Recommendation: add watcher on
main.go
file to rewritemain.wasm
on every "save" eventAlso you can run
build
command in code:const { build } = require('nodejs-golang'); ... await build();
To make result of
main.go
execution available for Node.js - finish you code withfmt.Print(MY_RESULT)
to write it to stdoutTo get result of
main.wasm
execution - userun
functionconst { run } = require('nodejs-golang'); ... const MY_RESULT = await run();
Multiple Golang scripts
Since nodejs-golang version 0.0.4 there is possibility to create, build and run multiple golang scripts in one project
GO_MODULE_NAME
is the Node.js environment variable for initialization and building needed script in go_modules
directory
Module initialization:
GO_MODULE_NAME=my_go_module node ./node_modules/nodejs-golang/init.js
Also you can run
init
command in code:const { init } = require('nodejs-golang'); ... await init('my_go_module');
Directory
go_modules/my_go_module
will be createdIn
go_modules/my_go_module
directory will be createdmain.go
file. Example:package main import ( "fmt" ) func main() { fmt.Print("Hello, go 1.16.2") }
Default
main.wasm
file will be compiled (also can be found ingo_modules/my_go_module
directory)- You need to put you code into
main.go
file To rewrite
main.wasm
with actual info run:GO_MODULE_NAME=my_go_module node ./node_modules/nodejs-golang/build.js
Recommendation: add watcher on
main.go
files to rewritemain.wasm
on every "save" eventAlso you can run
build
command in code:const { build } = require('nodejs-golang'); ... await build('my_go_module');
To make result of
main.go
execution available for Node.js - finish you code withfmt.Print(MY_RESULT)
to write it to stdoutTo get result of
main.wasm
execution - userun
functionconst { run } = require('nodejs-golang'); ... const MY_RESULT = await run('my_go_module');
Use Golang functions in Node.js
Since nodejs-golang version 0.0.5 there is possibility to use Golang functions in Node.js
Recommendation: because go functions will be set as global better to use specific naming.
Example: go_MY_FUNCTION_NAME
In
main.go
file create a function like this (there are some requirements):package main import ( "syscall/js" ) func myFunction(...) interface{} { ... return js.ValueOf(MY_VALUE) } func main() { c := make(chan struct{}, 0) js.Global().Set("go_MY_FUNCTION_NAME", js.FuncOf(myFunction)) <-c }
Requirements:
a) there must be an import of "syscall/js"
b) "myFunction" must be set to "global" js object with some name (better to keep some naming convention). For example:
go_MY_FUNCTION_NAME
c) use
make
function to initialize channel"instantiate" method should be run with the name of your module as a parameter while application start (or just before you need to use Golang function)
const { instantiate } = require('nodejs-golang'); ... await instantiate('my_go_module');
You can use methods from Golang anywhere in your application
const MY_VALUE = go_MY_FUNCTION_NAME();
Support
Linux only, Node.js 14+, Go 1.16.2