"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How I build simple Mac apps using Go

How I build simple Mac apps using Go

Published on 2024-07-29
Browse:841

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
        })
    })
}

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!

Release Statement This article is reproduced at: https://dev.to/progrium/how-i-build-simple-mac-apps-using-go-104j?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3