Skip to content

Instantly share code, notes, and snippets.

@4ydx
Forked from lloeki/main.go
Created February 13, 2019 06:50
Show Gist options
  • Save 4ydx/6528cbb0f145108a66d6ce5bac831b5a to your computer and use it in GitHub Desktop.
Save 4ydx/6528cbb0f145108a66d6ce5bac831b5a to your computer and use it in GitHub Desktop.
Using GopherJS and Electron together
// Electron's quick start sample, naively ported to Go
// https://github.com/atom/electron/blob/master/docs/tutorial/quick-start.md
//
// go get -u gopherjs
// gopherjs build main.go
// electron $(pwd)
package main
import (
"fmt"
"github.com/gopherjs/gopherjs/js"
"path"
"runtime"
)
func main() {
fmt.Printf("js: %v\n", js.Global.Get("process").Get("version"))
fmt.Printf("electron: %v\n", js.Global.Get("process").Get("versions").Get("electron"))
app := js.Global.Call("require", "app")
browserWindow := js.Global.Call("require", "browser-window")
crashReporter := js.Global.Call("require", "crash-reporter")
crashReporter.Call("start")
var mainWindow *js.Object
app.Call("on", "window-all-closed", func() {
if js.Global.Get("process").Get("platform").String() != "darwin" {
app.Call("quit")
}
})
app.Call("on", "ready", func() {
geom := map[string]int{"width": 800, "height": 600}
mainWindow = browserWindow.New(geom)
// and load the index.html of the app.
_, filename, _, _ := runtime.Caller(1)
url := "file://" + path.Join(path.Dir(filename), "index.html")
mainWindow.Call("loadUrl", url)
// Emitted when the window is closed.
mainWindow.Call("on", "closed", func() {
mainWindow = nil
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment