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!
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() {
@@emilywilliams6403 Change the signature of the function you want to register using FuncOf 1. Add a parameter of type js.Value 2. The function must return an empty interface (I think this will change in the future) From: func add(i []js.Value) { ...... } To: func add(this js.Value, i []js.Value) interface{} { ...... return 'something'} Hope it helps!
Thanks! Im just getting started with Go, what would be a more practical use case for this where plain old JS wouldnt cut it? Would it mainly be multi threaded tasks?
@@GifCoDigital Use cases are very very limited. If you want nano-second performance like some 3D games or some image editing application. etc Not for 99% of web apps i guess.
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.
I agree, the js.Global().Set("output", ...) line is useless. What this line does is it sets the value of the Javascript variable 'output'. But he doesn't use the 'output' variable anywhere in this tutorial, so its only confusing. I liked the rest of the tutorial though. Thanks for making it.
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
This was something that I too encountered when upgrading to Go v1.11. Ensure your GOPATH and GOROOT are both pointed to your new Go 1.11 installation and that when you are performing the build, it is using the appropriate GOARCH and GOOS settings. Give this a shot and let me know how you get on :)
It's still an experimental port for now, but when it becomes more mature I can definitely envision new frameworks coming out that are built purely in Go. I can see these simplifiying the way we do things within our web apps and providing us a lot more flexibility and freedom.
Iris web framework has already an example on its repository for webassemply + javascript + iris backend API, you may want to take a look at github.com/kataras/iris/tree/master/_examples
(index):1 Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #5 module="go" function="runtime.scheduleCallback" error: function import requires a callable. Anyone helps me resolve this error??
Found a solution, dont use the wasm_exec.js in the description. Its repo has been changed. Just use the one in his project. github.com/golang/go/edit/master/misc/wasm/wasm_exec.js It worked for me.
But it does. Compile the server.go with GOARCH=amd64 and GOOS=windows and run the server. After that change the envs GOARCH=wasm GOOS=js and compile the main.go to main.wasm. Be shure to change the name of the wasm file in the index.html
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", i[0].String()).Get("value").String() value2 := js.Global().Get("document").Call("getElementById", i[1].String()).Get("value").String() 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)
The Source Code for this entire project can be found here: github.com/TutorialEdge/Go/tree/master/go-webassembly-tutorial
Dead link.
Broken link. The correct one seems to be: github.com/elliotforbes/go-webassembly-framework
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!
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
the example don't run on go 1.12 ,because 1.12 have delete "js.NewCallback" API, and instead of Funccof
even when I change to funcOf i'm getting some weird errors :/
@@emilywilliams6403 Change the signature of the function you want to register using FuncOf
1. Add a parameter of type js.Value
2. The function must return an empty interface (I think this will change in the future)
From: func add(i []js.Value) { ...... }
To: func add(this js.Value, i []js.Value) interface{} { ...... return 'something'}
Hope it helps!
Congrats on the Awesome Go shoutout!
What theme with arrows in terminal is used?
Oh my zsh default theme, 'robbyrussell'
So stupid questions.... the actual function calls are running on the client right? Not the server?
It's not a stupid question at all, these functions are indeed executed within the client and not the server.
Thanks! Im just getting started with Go, what would be a more practical use case for this where plain old JS wouldnt cut it? Would it mainly be multi threaded tasks?
@@GifCoDigital
Use cases are very very limited. If you want nano-second performance like some 3D games or some image editing application. etc Not for 99% of web apps i guess.
I cant fint the split temp terminal.
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.
I agree, the js.Global().Set("output", ...) line is useless. What this line does is it sets the value of the Javascript variable 'output'. But he doesn't use the 'output' variable anywhere in this tutorial, so its only confusing.
I liked the rest of the tutorial though. Thanks for making it.
glad I'm not the only one going crazy at this part. I'm getting lots of errors too I think more than one thing might be outdated.
Thank you so much!
Can this run on windows 10 home ? I feel there are many errors
Is this hosted anywhere so that I can test it before I try the tutorial? Or any other wasm example written in go?
Here's the repo for this particular tutorial - github.com/TutorialEdge/Go/tree/master/go-webassembly-tutorial
@@Tutorialedge would be great if you pin it. Thx
I found the repo, I was asking if I could try it in the browser first
Great, thanks!
Nice!
i need help regarding .WASM file??
What ide/texteditor is this?
visual studio code
It's visual studio code! :)
correct link : tutorialedge.net/golang/go-webassembly-tutorial/
Good catch! I've updated the link :) Thanks for your help!
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
This was something that I too encountered when upgrading to Go v1.11. Ensure your GOPATH and GOROOT are both pointed to your new Go 1.11 installation and that when you are performing the build, it is using the appropriate GOARCH and GOOS settings. Give this a shot and let me know how you get on :)
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 ?
It's still an experimental port for now, but when it becomes more mature I can definitely envision new frameworks coming out that are built purely in Go.
I can see these simplifiying the way we do things within our web apps and providing us a lot more flexibility and freedom.
Iris web framework has already an example on its repository for webassemply + javascript + iris backend API, you may want to take a look at github.com/kataras/iris/tree/master/_examples
Goolobal 😀
Could you provide source code please? Thx.
github.com/TutorialEdge/Go/tree/master/go-webassembly-tutorial - here you go! :D
@@Tutorialedge
Thats not working.
also Go 1.12 nothing works. I think still way ahead.
(index):1 Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #5 module="go" function="runtime.scheduleCallback" error: function import requires a callable. Anyone helps me resolve this error??
same issue
Found a solution, dont use the wasm_exec.js in the description. Its repo has been changed. Just use the one in his project.
github.com/golang/go/edit/master/misc/wasm/wasm_exec.js
It worked for me.
Woooooooow
Interesting
of course this doesn't work on windows
But it does. Compile the server.go with GOARCH=amd64 and GOOS=windows and run the server. After that change the envs GOARCH=wasm GOOS=js and compile the main.go to main.wasm. Be shure to change the name of the wasm file in the index.html
wow
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", i[0].String()).Get("value").String()
value2 := js.Global().Get("document").Call("getElementById", i[1].String()).Get("value").String()
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)