Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shortest GUI program written in Golang. It displays a window and exits when the "close" button of the window is clicked.
// Shortest GUI program written in Golang.
// It displays a window and exits when the "close" button of the window is clicked.
package main
import (
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
// Despite that the package names have a "mobile" prefix,
// these packages works on desktop.
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
)
func main() {
driver.Main(func(s screen.Screen) {
w, err := s.NewWindow(nil)
if err != nil {
panic(err)
}
defer w.Release()
for {
switch e := w.NextEvent().(type) {
case size.Event:
if e.WidthPx == 0 && e.HeightPx == 0 {
return
}
case paint.Event:
w.Publish()
case lifecycle.Event:
if e.To == lifecycle.StageDead {
return
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment