The story behind the Mercurius framework
In August of this year, I went to a Go metup here in Brazil more …
It is increasingly common backend languages have a compiler for javaScript, enabling us to use the same code in the backend and in the browser.
Clojure, Scala and kotlin already support to compile your code for JavaScript, in Go it’s completely possible too.
In this post we will see how to do it.
GopherJS
to our GOPATH
go get -u github.com/gopherjs/gopherjs
package main
import (
"github.com/gopherjs/gopherjs/js"
"fmt"
)
func main() {
js.Global.Get("document").Call("write", "Say hello to GopherJS")
fmt.Println("GopherJS")
}
In the first line of main function, we get the document
JavaScript object and call your method write
passing the string Say hello to GopherJS
as argument. In the second line we are doing the same as console.log("GopherJS")
gopherjs build -o app.js
.js
file passing -m
as argumentgopherjs build -m -o app.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Intro GopherJS</title>
</head>
<body>
<script src="js/app.js"></script>
</body>
</html>
Now we finish it. We can startup our server and see it run on browser.
This post project is on my github see you in next post