0.0.5 • Published 4 years ago

nodejs-golang v0.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

nodejs-golang

Module for running Golang scripts using wasm in Node.js child processes

How It Works

  1. During module installation npm i nodejs-golang Go programming language will be downloaded and installed
  2. In the root of the projects will be created default go_modules directory
  3. In go_modules directory will be created main.go file. Example:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        fmt.Print("Hello, go 1.16.2")
    }
  4. Default main.wasm file will be compiled (also can be found in go_modules directory)

  5. You need to put you code into main.go file
  6. To rewrite main.wasm with actual info run:

    node ./node_modules/nodejs-golang/build.js

    Recommendation: add watcher on main.go file to rewrite main.wasm on every "save" event

    Also you can run build command in code:

    const { build } = require('nodejs-golang');
    ...
    await build();
  7. To make result of main.go execution available for Node.js - finish you code with fmt.Print(MY_RESULT) to write it to stdout

  8. To get result of main.wasm execution - use run function

    const { 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

  1. 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');
  2. Directory go_modules/my_go_module will be created

  3. In go_modules/my_go_module directory will be created main.go file. Example:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        fmt.Print("Hello, go 1.16.2")
    }
  4. Default main.wasm file will be compiled (also can be found in go_modules/my_go_module directory)

  5. You need to put you code into main.go file
  6. 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 rewrite main.wasm on every "save" event

    Also you can run build command in code:

    const { build } = require('nodejs-golang');
    ...
    await build('my_go_module');
  7. To make result of main.go execution available for Node.js - finish you code with fmt.Print(MY_RESULT) to write it to stdout

  8. To get result of main.wasm execution - use run function

    const { 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

  1. 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

  2. "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');
  3. 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