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-golangGo programming language will be downloaded and installed - In the root of the projects will be created default
go_modulesdirectory In
go_modulesdirectory will be createdmain.gofile. Example:package main import ( "fmt" ) func main() { fmt.Print("Hello, go 1.16.2") }Default
main.wasmfile will be compiled (also can be found ingo_modulesdirectory)- You need to put you code into
main.gofile To rewrite
main.wasmwith actual info run:node ./node_modules/nodejs-golang/build.jsRecommendation: add watcher on
main.gofile to rewritemain.wasmon every "save" eventAlso you can run
buildcommand in code:const { build } = require('nodejs-golang'); ... await build();To make result of
main.goexecution available for Node.js - finish you code withfmt.Print(MY_RESULT)to write it to stdoutTo get result of
main.wasmexecution - userunfunctionconst { 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.jsAlso you can run
initcommand in code:const { init } = require('nodejs-golang'); ... await init('my_go_module');Directory
go_modules/my_go_modulewill be createdIn
go_modules/my_go_moduledirectory will be createdmain.gofile. Example:package main import ( "fmt" ) func main() { fmt.Print("Hello, go 1.16.2") }Default
main.wasmfile will be compiled (also can be found ingo_modules/my_go_moduledirectory)- You need to put you code into
main.gofile To rewrite
main.wasmwith actual info run:GO_MODULE_NAME=my_go_module node ./node_modules/nodejs-golang/build.jsRecommendation: add watcher on
main.gofiles to rewritemain.wasmon every "save" eventAlso you can run
buildcommand in code:const { build } = require('nodejs-golang'); ... await build('my_go_module');To make result of
main.goexecution available for Node.js - finish you code withfmt.Print(MY_RESULT)to write it to stdoutTo get result of
main.wasmexecution - userunfunctionconst { 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.gofile 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_NAMEc) use
makefunction 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