DEV Community

Cover image for How I build simple Mac apps using Go
Jeff Lindsay
Jeff Lindsay

Posted on

How I build simple Mac apps using Go

I started DarwinKit a few years ago because there were no bindings to native Mac APIs for Go. We've slowly turned the project into bindings and generation tooling to someday reach full coverage of all Apple APIs. The release of v0.5.0 last week is the largest the project has seen:

  • Bindings for 33 frameworks with near complete coverage:
    • 2,353 classes
    • 23,822 methods and properties
    • 9,519 constants/enums
    • 543 structs
  • Automatic conversion and use of native Go builtin types in APIs
  • Support for block arguments as Go functions with properly typed arguments
  • Pre-made delegate implementations you can simply set Go functions on
  • 1-to-1 mapping to Objective-C symbols while still idiomatic to Go
  • Documentation for all symbols including a link to official Apple docs on that symbol
  • Growing collection of high-quality example starter apps for sponsors

Demo Screenshots

Here is a quick example using DarwinKit to build a native webview window application in a few lines of Go:

package main

import (
    "github.com/progrium/darwinkit/objc"
    "github.com/progrium/darwinkit/macos"
    "github.com/progrium/darwinkit/macos/appkit"
    "github.com/progrium/darwinkit/macos/foundation"
    "github.com/progrium/darwinkit/macos/webkit"
)

func main() {
    // runs macOS application event loop with a callback on success
    macos.RunApp(func(app appkit.Application, delegate *appkit.ApplicationDelegate) {
        app.SetActivationPolicy(appkit.ApplicationActivationPolicyRegular)
        app.ActivateIgnoringOtherApps(true)

        url := foundation.URL_URLWithString("https://github.com/sponsors/darwinkitdev")
        req := foundation.NewURLRequestWithURL(url)
        frame := foundation.Rect{Size: foundation.Size{1440, 900}}

        config := webkit.NewWebViewConfiguration()
        wv := webkit.NewWebViewWithFrameConfiguration(frame, config)
        wv.LoadRequest(req)

        w := appkit.NewWindowWithContentRectStyleMaskBackingDefer(frame,
            appkit.ClosableWindowMask|appkit.TitledWindowMask,
            appkit.BackingStoreBuffered, false)
        objc.Retain(&w)
        w.SetContentView(wv)
        w.MakeKeyAndOrderFront(w)
        w.Center()

        delegate.SetApplicationShouldTerminateAfterLastWindowClosed(func(appkit.Application) bool {
            return true
        })
    })
}
Enter fullscreen mode Exit fullscreen mode

In less than 40 lines we made a native Mac app without opening XCode or using Objective-C. I think this might now be the best bindings project in existence for Apple APIs. Possibly even the best way to make small utilities on the Mac. And soon even other Apple devices.

The Future

There is one big missing piece to DarwinKit: there are no bindings to Apple framework functions. Luckily, most frameworks are built with OOP, which we have great bindings for now. But some frameworks, especially lower-level frameworks, are mostly functions. While there is a workaround that involves using CGO (which DarwinKit is trying to help you avoid), we're working on generating native Go function bindings for every framework function.

The other big thing we're working towards is making DarwinKit not use CGO at all! Using purego, we can call into Apple frameworks without involving CGO. This will improve build time, make smaller binaries, and allow DarwinKit to be used in programs that need to avoid CGO for whatever reason.

For iOS and mobile devs out there, I really want to get this working for iOS. In fact, it already should! But we generate bindings for MacOS for now. If anybody wants to help bring this to iOS to let people make Apple mobile apps with Go, please reach out!

Until then, try building an app with what we've got so far. Let me know how it goes!

Top comments (7)

Collapse
 
oaklandgit profile image
Larry Stone

Exciting! Can’t wait to give it a whirl.

Collapse
 
louis1995 profile image
Trương Văn Lộc

I have one question, what would happen if Apple update their framework?
Will you have to release a patch for that as well, because the code interface might be changed.

Collapse
 
progrium profile image
Jeff Lindsay

While Apple changes things quite often, they typically add more than they change or remove. Deprecated APIs stick around for quite a while. Most of the AppKit APIs haven't changed since they were part of NeXTSTEP in the 90s. That said we try to stay within a release of what's released and test against the supported versions of macOS. If a binding is wrong or missing, you can still use the lower level Objective-C bindings to call a method with the correct signature.

Collapse
 
mannyuncharted profile image
Emmanuel Akanji

A good question to ask

Collapse
 
mannyuncharted profile image
Emmanuel Akanji

This is interesting can't wait to try it out.

Collapse
 
stankukucka profile image
Stan Kukučka

@progrium how the go code is saved or compiled to the MacOS application?

Collapse
 
progrium profile image
Jeff Lindsay

MacOS applications are just executables in an app bundle. You compile the Go to an executable and put it in a bundle: stackoverflow.com/a/3251285

Though this is usually optional, you can just run the executable most of the time.