Goroutines

GopherJS supports goroutines, so you can use goroutines in your Go code, and GopherJS will take care of the rest. 

One important requirement is that goroutines must be used if you need to call some blocking code from an external JavaScript.

For example, consider the following JavaScript code running in the browser:

document.getElementById("myBtn").addEventListener("click", function(){
/*SOME BLOCKING CODE*/
});

The preceding code defines a callback function that is expected to execute when a button is clicked.

Here is how this should be handled in Go with the help of GopherJS:

js.Global.Get("document").Call("getElementById", "mybtn").Call("addEventListener","call", func() {
go func() {
/*SOME BLOCKING CODE*/
}()
})

As shown in the preceding code snippet, we had to use a goroutine inside the event listener callback code, because it was expected to run some blocking code.

Now that we know the fundamentals of GopherJS, let's use GopherJS with React.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181