GopherJS
at Helsinki Gophers meetup
11 May 2016
Timo Savola
CTO, Ninchat
Timo Savola
CTO, Ninchat
go get github.com/gopherjs/gopherjs gopherjs build
Fully supported: 110
Partially supported: 12
net/http
os
and syscall
supported on Node.jsNot supported: 21
unsafe
package main import "github.com/gopherjs/gopherjs/js" var doc = js.Global.Get("document") func main() { div := doc.Call("createElement", "div") div.Set("innerText", "hello world") doc.Get("body").Call("appendChild", div) }
const MyString = "hello world" func MyApplyFunc(callable *js.Object, arg string) { if callable != js.Undefined { callable.Invoke(arg) } } func main() { js.Global.Get("window").Set("MyStuff", map[string]interface{}{ "MY_STRING": MyString, "myApplyFunc": MyApplyFunc, }) }
> MyStuff.myApplyFunc(someUnaryFunction, "asdf")
type ValueThing struct { Foo int Bar string Baz []uint16 } func NewValueThing() *ValueThing { return &ValueThing{} } func (t *ValueThing) GetSomething() int { return 42 }
> t = examples.NewValueThing()
> t
Object {Foo: 0, Bar: "", Baz: Array[0]}
> t.GetSomething()
Uncaught TypeError: t.GetSomething is not a function
type ActiveThing struct { Foo int Bar string Baz []uint16 } func NewActiveThing() *js.Object { return js.MakeWrapper(&ActiveThing{}) } func (t *ActiveThing) GetSomething() int { return 42 }
> t = examples.NewActiveThing()
> t
Object {internal stuff only}
> t.GetSomething()
42
Doesn't work:
import "net/http" func MyExportedFunc1(url string) { r, _ := http.Get(url) // blocking call println(r) }
Works:
import "net/http" func MyExportedFunc2(url string) { go func() { r, _ := http.Get(url) // blocking call println(r) }() }
mycode_js.go
// +build js import "github.com/gopherjs/gopherjs/js" func Display(message string) { js.Global.Get("console").Call("log", message) }
mycode_default.go
// +build !js import "fmt" import "os" func Display(message string) { fmt.Fprintln(os.Stderr, message) }
Empty main function:
Import the sort
package:
Import the fmt
package:
https://github.com/ninchat/ninchat-go
is an importable Go packagehttps://github.com/ninchat/ninchat-js
is the main packageCode base:
File size:
Estimated file size without direct browser API calls:
Ninchat profiling:
Internet says:
fmt
package (etc.) if file size matters