Hello guys!
I’m utilizing a Go open supply library with my venture.
Our complete initiatives are written in JS, so I wish to use the entire Go library in some JS codes.
Is there anybody who is aware of and recommends any correct(environment friendly) manner to make use of Go in JS or with JS ?
Thanks upfront,
Hoodie
A technique is to make use of Javascript fetch() to speak with a Go http server with endpoints.
This writes the specified Go output right into a webpage by utilizing innerHTML:
perform ask_go() {
let url = "/askgo"
fetch(url)
.then((res) => res.textual content())
.then((response) => {
doc.getElementById("content material").innerHTML = response;
})
.catch((error) => alert("Error:", error));
}
Hello Sibert,
I would like some a lot deeper use of the Go library, similar to use of capabilities of it or file itself with(in) JS.
However anyway, thanks!
I’m additionally on the lookout for methods to make use of Go together with Javascript. However I’ve discovered no different approach to talk between an interpreted and compiled language (they don’t share the identical engine) than some type of micro companies. I e ship messages backwards and forwards. I must be glad confirmed incorrect.
1 Like
Have you ever already regarded into
- GopherJS (ie, compiling Go to JS), or
- compiling Go to WASM?
With out having regarded deeper into any of those possibility, I’d reckon that these might be appropriate methods of calling Go libraries from JS.
Edited so as to add:
GopherJS permits calling Go code from JS.
WebAssembly permits calling something that’s compiled right into a WASM (WebAssembly module) from JavaScript.
TinyGo is especially suited to WASM due to its smaller runtime footprint. (Article about calling WASM libraries from JS.)
Facet notice: The Go venture wazero
goes into the opposite route: name WASMs from Go.
(Notice to self: I ought to lastly begin wrapping my head round WebAssembly.)
2 Likes
Hello Christoph,
As you mentioned, I additionally discovered these choices + compiling Go packages into C shared libraries(Calling Go Features from Different Languages | by Vladimir Vivien | Studying the Go Programming Language | Medium) !
So if another higher possibility doesn’t exist, it’d be higher working deeply with one(or some) of them!
Thanks all
1 Like
May you please give some extra element of your anticipated structure – i.e are you utilizing JS from browser or NodeJS. Do you wish to run Go on the identical machine? Are you utilizing cellular/desktop/gadgets? What O/S (shouldn’t matter although)?
e.g. I take advantage of browser JS with a Go http server (home windows) utilizing REST – and likewise utilizing internet sockets for push from server. For this I take advantage of normal Go server.http with mux and websocket. I additionally use getlantern/systray to have an icon you should use to open chrome, utilizing lorca, after the server is working, i.e for an editor in my case. Notice that I solely want max 60 body/s updates which will not be your case…
Finest needs – Andy