Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Run JavaScript in GoLang

  sonic0002        2019-05-16 07:40:38       14,508        0    

In some cases, there might be some JavaScript code needs to be ran in a GoLang project. Fortunately, there are a few libraries which support running JavaScript in GoLang. The most famous one would be v8. This is a Go API for the famous v8 JavaScript engine originally developed for the Chrominum project and the foundation of NodeJS.

In this post, we will show some simple steps to get v8 working in a GoLang program on MacOS. First you need to install the package so that you can import it. Run below

go get github.com/augustoroman/v8

Unluckily you would encounter an error where some header file cannot be found in Linux environment. 

go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src;
	ignoring go.mod;
	see 'go help modules'
# github.com/augustoroman/v8
v8_c_bridge.cc:3:10: fatal error: 'libplatform/libplatform.h' file not found

Don't panic, the README file from this repo provides some ways to solve this problem. The easiest would be installing the precompiled v8 gem as suggested but with some modification.

# Find the appropriate gem version for your OS,
# visit: https://rubygems.org/gems/libv8/versions

# create some directory in your OS, diffrent from where you put your go packages
mkdir -p /some/path/to/v8
cd /some/path/to/v8


# Download the gem
# MacOS Sierra is darwin-16, for v8 6.3.292.48.1 it looks like: 
cur l https://rubygems.org/downloads/libv8-6.3.292.48.1-x86_64-darwin-16.gem > libv8.gem

# Extract the gem (it's a tarball)
tar -xf libv8.gem

# Extract the `data.tar.gz` within (For below, you may find that the above gem file is extracted to the same directory in some OS, so no need to cd, just find the daata.tar.gz in the same directory)
cd libv8-6.3.292.48.1-x86_64-darwin-16
tar -xzf data.tar.gz

# Symlink the compiled libraries and includes. (For below, you may find that the above gem file is extracted to the same directory in some OS, so no need to add data in below link path)
ln -s $(pwd)/data/vendor/v8/include $GOPATH/src/github.com/augustoroman/v8/include
ln -s $(pwd)/data/vendor/v8/out/x64.release $GOPATH/src/github.com/augustoroman/v8/libv8

# Run the tests to make sure everything works
cd $GOPATH/src/github.com/augustoroman/v8
go test

OK. After this, you should see that the testing will pass. This indicates v8 is successfully installed.

Now start to write some simple go code to prove that.

package main

import (
	"fmt"

	v8 "github.com/augustoroman/v8"
)

func main() {
	ctx := v8.NewIsolate().NewContext()

	ctx.Eval(`
	   add = (a,b)=>{ return a + b };
	`, "add.js")

	res, _ := ctx.Eval(`add(3,4)`, "compute.js")
	fmt.Println("add(3,4) =", res.String())
}

The above code is just to create a new instance of the v8 context and create a new function named add(a, b) which adds two numbers. Then the next Eval call will just call this method and it would return the computed result.

add(3,4) = 7

Note the Eval() function can take dynamically generated code or file. If it's dynamically generated code, the second parameter is a file name which just for debugging purpose in case some stack trace would be printed. 

V8  JAVASCRIPT  GOLANG 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.