Go WebAssembly Tutorial - Building a Calculator

preview_player
Показать описание

● SUBSCRIBE to see more of my Videos & hit that LIKE button to support the channel!

Hi Everyone, in this tutorial, we are going to be looking at how you can compile Go to WebAssembly and subsequently use that to power your frontend applications with JavaScript.

We'll be building a really simple calculator and exposing a couple of Go based functions that can subsequently be called within our frontend using the onClick attribute.



‎--------------

🎥 Recording Setup

Рекомендации по теме
Комментарии
Автор

Depending on your version of Go, you might have to do some fiddling with the 'server.go' file in order to run this and export the lib.wasm properly. For me, I had to change the main function in 'server.go' to another function name, ("runserver()"), for instance. Then, you'll need to call 'runServer()' in the main.go file, under the main function.


Finally, when compiling the lib.wasm, file you'll need to run ALL of the go files in that directory. You're command line should look like this:
GOARCH=wasm GOOS=js go build -o lib.wasm *.go


Let me know if that works for you all!

josephkeller
Автор

the example don't run on go 1.12, because 1.12 have delete "js.NewCallback" API, and instead of Funccof

jlhl
Автор

Hi Elliot
Can you kindly update the flowing function according to go version 1.12.3? because js.NewCallback() had been drop. I couldn’t work around
.
func registerCallbacks() {
 
js.Global().Set("add", js.NewCallback(add))
js.Global().Set("subtract", js.NewCallback(subtract))
}

thanks

mahansathees
Автор

I'd like to know the purpose of this line:
js.Global().Set("output", ...);
What is it doing? You have another println() called under it to output the result.

vencelam
Автор

What theme with arrows in terminal is used?

Andrew-ukoo
Автор

Thank you very much.

The tutorial is too legacy, but despite it I've managed to make a working project.
You need to update next:

Make a register of callback
func registerCallbacks() {
js.Global().Set("add", js.FuncOf(add))
}

And update your add function with next:
func subtract(this js.Value, i []js.Value) interface{} {
value1 := js.Global().Get("document").Call("getElementById",
value2 := js.Global().Get("document").Call("getElementById",
intVal1, _ := strconv.Atoi(value1)
intVal2, _ := strconv.Atoi(value2)

println(intVal1 - intVal2)
js.Global().Get("document").Call("getElementById", i[2].String()).Set("value", intVal1 - intVal2)
return (intVal1 - intVal2)

alexeygurzhiy
Автор

So stupid questions.... the actual function calls are running on the client right? Not the server?

GifCoDigital
Автор

I checked the js dir there. But the build cmd always printed error.
main.go:5:2: build constraints exclude all Go files in D:\software\GO\src\syscall\js

tommyye
Автор

Can this run on windows 10 home ? I feel there are many errors

davidle
Автор

Is this hosted anywhere so that I can test it before I try the tutorial? Or any other wasm example written in go?

MenkoDany
Автор

Would you see this in prod as opposed to having an API written in Go and a (what ever flavour of the month) JS client ?

touristtam
Автор

(index):1 Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #5 module="go" error: function import requires a callable. Anyone helps me resolve this error??

khaioan
Автор

Could you provide source code please? Thx.

dz_s
Автор

of course this doesn't work on windows

edwardanugent