Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
Thanks to go-astilectron build cross platform GUI apps with GO and HTML/JS/CSS. It is the official GO bindings of astilectron and is powered by Electron.
This project is not maintained anymore.
To see a minimal Astilectron app, checkout out the demo.
It uses the bootstrap and the bundler.
If you're looking for a minimalistic example, run go run example/main.go -v.
Here's a list of awesome projects using go-astilectron (if you're using go-astilectron and want your project to be listed here please submit a PR):
For convenience purposes, a bootstrap has been implemented.
The bootstrap allows you to quickly create a one-window application.
There's no obligation to use it, but it's strongly recommended.
If you decide to use it, read thoroughly the documentation as you'll have to structure your project in a specific way.
Still for convenience purposes, a bundler has been implemented.
The bundler allows you to bundle your app for every os/arch combinations and get a nice set of files to send your users.
WARNING: the code below doesn't handle errors for readibility purposes. However you SHOULD!
go-astilectronTo import go-astilectron run:
$ go get -u github.com/asticode/go-astilectrongo-astilectron…For everything to work properly we need to fetch 2 dependencies : astilectron and Electron. .Start() takes care of it by downloading the sources and setting them up properly.
In case you want to embed the sources in the binary to keep a unique binary you can use the NewDisembedderProvisioner function to get the proper Provisioner and attach it to go-astilectron with .SetProvisioner(p Provisioner). Or you can use the bootstrap and the bundler. Check out the demo to see how to use them.
Beware when trying to add your own app icon as you'll need 2 icons : one compatible with MacOSX (.icns) and one compatible with the rest (.png for instance).
If no BaseDirectoryPath is provided, it defaults to the executable's directory path.
The majority of methods are asynchronous which means that when executing them go-astilectron will block until it receives a specific Electron event or until the overall context is cancelled. This is the case of .Start() which will block until it receives the app.event.ready astilectron event or until the overall context is cancelled.
NB! All paths in HTML (and Javascript) must be relative, otherwise the files will not be found. To make this happen in React for example, just set the homepage property of your package.json to "./".
{ "homepage": "./" }
// Create a new window
var w, _ = a.NewWindow("http://127.0.0.1:4000", &astilectron.WindowOptions{
Center: astikit.BoolPtr(true),
Height: astikit.IntPtr(600),
Width: astikit.IntPtr(600),
})
w.Create()When creating a window you need to indicate a URL as well as options such as position, size, etc.
This is pretty straightforward except the astilectron.Ptr* methods so let me explain: GO doesn't do optional fields when json encoding unless you use pointers whereas Electron does handle optional fields. Therefore I added helper methods to convert int, bool and string into pointers and used pointers in structs sent to Electron.
When developing in JS, it's very convenient to debug your code using the browser window's dev tools:
// Open dev tools
w.OpenDevTools()
// Close dev tools
w.CloseDevTools()// Add a listener on Astilectron
a.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
log.Println("App has crashed")
return
})
// Add a listener on the window
w.On(astilectron.EventNameWindowEventResize, func(e astilectron.Event) (deleteListener bool) {
log.Println("Window resized")
return
})Nothing much to say here either except that you can add listeners to Astilectron as well.
// Play with the window
w.Resize(200, 200)
time.Sleep(time.Second)
w.Maximize()Check out the Window doc for a list of all exported methods
// This will wait for the astilectron namespace to be ready
document.addEventListener('astilectron-ready', function() {
// This will listen to messages sent by GO
astilectron.onMessage(function(message) {
// Process message
if (message === "hello") {
return "world";
}
});
})// This will send a message and execute a callback
// Callbacks are optional
w.SendMessage("hello", func(m *astilectron.EventMessage) {
// Unmarshal
var s string
m.Unmarshal(&s)
// Process message
log.Printf("received %s\n", s)
})This will print received world in the GO output
// This will listen to messages sent by Javascript
w.OnMessage(func(m *astilectron.EventMessage) interface{} {
// Unmarshal
var s string
m.Unmarshal(&s)
// Process message
if s == "hello" {
return "world"
}
return nil
})// This will wait for the astilectron namespace to be ready
document.addEventListener('astilectron-ready', function() {
// This will send a message to GO
astilectron.sendMessage("hello", function(message) {
console.log("received " + message)
});
})This will print "received world" in the Javascript output
// Clear window's HTTP cache
w.Session.ClearCache()// If several displays, move the window to the second display
var displays = a.Displays()
if len(displays) > 1 {
time.Sleep(time.Second)
w.MoveInDisplay(displays[1], 50, 50)
}…A few things to know:
go-astilectron won't be able to capture its click event………Registering a global shortcut.
// Register a new global shortcut
isRegistered, _ := a.GlobalShortcuts().Register("CmdOrCtrl+x", func() {
fmt.Println("CmdOrCtrl+x is pressed")
})
fmt.Println("CmdOrCtrl+x is registered:", isRegistered) // true
// Check if a global shortcut is registered
isRegistered, _ = a.GlobalShortcuts().IsRegistered("Shift+Y") // false
// Unregister a global shortcut
a.GlobalShortcuts().Unregister("CmdOrCtrl+x")
// Unregister all global shortcuts
a.GlobalShortcuts().UnregisterAll()Add the following line at the top of your javascript file :
const { dialog } = require('electron').remoteUse the available methods.
// Listen to login events
w.OnLogin(func(i astilectron.Event) (username, password string, err error) {
// Process the request and auth info
if i.Request.Method == "GET" && i.AuthInfo.Scheme == "http://" {
username = "username"
password = "password"
}
return
})go-thrust which is awesome but unfortunately not maintained anymore. It inspired this project.
No open issues yet, or sync has not completed.