How to use Go library in JavaScript?

Hi guys!

I’m using a Go open source library with my project.

Our entire projects are written in JS, so I want to use the whole Go library in some JS codes.

Is there anyone who knows and recommends any proper(efficient) way to use Go in JS or with JS ?

Thank you in advance,

Hoodie

One way is to use Javascript fetch() to communicate with a Go http server with endpoints.

This writes the desired Go output into a webpage by using innerHTML:

function ask_go() {
  let url = "/askgo"
  fetch(url)
    .then((res) => res.text())
    .then((response) => {
      document.getElementById("content").innerHTML = response;
    })
    .catch((error) => alert("Error:", error));
}

Hi Sibert,

I want some much deeper use of the Go library, such as use of functions of it or file itself with(in) JS.

But anyway, thanks!

I am also looking for ways to use Go with Javascript. But I have found no other way to communicate between an interpreted and compiled language (they do not share the same engine) than some sort of micro services. I e send messages back and forth. I should be glad proven wrong.

1 Like

Have you already looked into

  • GopherJS (ie, compiling Go to JS), or
  • compiling Go to WASM?

Without having looked deeper into any of these option, I would reckon that these could be suitable ways of calling Go libraries from JS.

Edited to add:

GopherJS allows calling Go code from JS.

WebAssembly allows calling anything that is compiled into a WASM (WebAssembly module) from JavaScript.

TinyGo is particularly suited for WASM because of its smaller runtime footprint. (Article about calling WASM libraries from JS.)


Side note: The Go project wazero goes into the other direction: call WASMs from Go.

(Note to self: I should finally start wrapping my head around WebAssembly.)

2 Likes

Hi Christoph,

As you said, I also found those options + compiling Go packages into C shared libraries(Calling Go Functions from Other Languages | by Vladimir Vivien | Learning the Go Programming Language | Medium) !

So if any other better option doesn’t exist, it’d be better working deeply with one(or some) of them!

Thank you all :slight_smile:

1 Like

That‘s a super useful article. Thanks for sharing, @hoodie!

1 Like

Could you please give some more detail of your expected architecture - i.e are you using JS from browser or NodeJS. Do you want to run Go on the same machine? Are you using mobile/desktop/devices? What O/S (shouldn’t matter though)?

e.g. I use browser JS with a Go http server (windows) using REST - and also using web sockets for push from server. For this I use standard Go server.http with mux and websocket. I also use getlantern/systray to have an icon you can use to open chrome, using lorca, after the server is running, i.e for an editor in my case. Note that I only need max 60 frame/s updates which may not be your case…

Best wishes - Andy

Hi Andy

I want to use NodeJS to run a Go library on the same mac machine.
So I’m trying to check the compatibility of the GopherJS and the Go library!

I’m guessing your concern is performance will suffer - but maybe you would be best off benchmarking a go http server and seeing what the performance is like, since the overhead may be insigificant - it really depends on how often you call the Go functions and how much work gets done by them.

You may also find you can send multiple requests at once which could benefit. Or turning a set of function calls into a single ‘service’.

I also recommend checking out tinygo - which is only a bit less than full go - but seems better for Wasm - which you could then use from nodejs directly - but, if you use wasm, you won’t get any performance benefit from using compiled Go.

Without knowing the library you are using and how frequently/what for, this is still uncertain - benchmarking should help.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.