I am trying to run JavaScript inside of a Golang function, and use fetch to fetch JSON via APIs in the Javascript context.
I tried it in Otto with the following code:
import "github.com/robertkrimen/otto"
vm := otto.New()
vm.Run(`
function tryfunc() {
console.log("Started");
fetch('https://www.example.com/APIendpoint');
console.log("Success");
}
tryfunc();
`)
Otto is very simple to use, but looks like Otto is an event bus and does not manage fetch.
I am now trying with v8go with the following code:
import v8 "rogchap.com/v8go"
ctx := v8.NewContext()
ctx.RunScript(`fetch("https://www.example.com/APIendpoint"), ""`)
but it requires another argument. The documentation is very unclear and is difficult to understand how to run even the simplest JS script.
Can someone help?