Skip to content

Instantly share code, notes, and snippets.

@andlabs
Last active August 29, 2015 13:56
Show Gist options
  • Save andlabs/9296024 to your computer and use it in GitHub Desktop.
Save andlabs/9296024 to your computer and use it in GitHub Desktop.

Most GUI toolkits require you to use one thread. It doesn't matter which thread, so long as you only use one thread.

Cocoa, however, absolutely demands you use the very first OS thread created, called the "main thread", and provides no way to change which thread is used.

This basically means that instead of you saying

func main() {
    w := ui.NewWindow(blah blah blah)
    blah blah blah
    for { event loop }
}

you must instead say

func myMain() {
    w := ui.NewWindow(blah blah blah)
    blah blah blah
    for { event loop }
}

func main() {
    ui.Go(myMain)
}

and ui.Go() runs myMain() as a goroutine. Or also

func main() {
    go myMain()
    ui.Go()
}

This also means you cannot use go test with package ui, as go test provides its own main().

I don't know if I want to do this; I'm not sure what other people think. I know it would be similar to http.ListenAndServe(), but.

Which do people generally prefer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment