Home >Backend Development >Golang >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:
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.
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!
The above is the detailed content of How I build simple Mac apps using Go. For more information, please follow other related articles on the PHP Chinese website!