36

Is it possible to write macOS/Cocoa applications in Google Go?

Is there a Go-Obj-C bridge? (it seems to me that Obj-C dynamism would be a great fit for Golang's interfaces)

Can I at least link the two together and make them talk to each other via plain-old C functions?

Kornel
  • 97,764
  • 37
  • 219
  • 309
  • Hahaha... Go's biggest weakpoint... interoperability. – Matt Joiner Jun 12 '11 at 13:54
  • ? It’s cocoa that’s mac-native, not Go. – Kissaki Jun 12 '11 at 13:57
  • 4
    "Hahaha" what? The "biggest weakpoint" comment does not make sense. And even if it did, there would be no "hahaha" about it. Go has an excellent C FFI and it's very easy to add Go bindings to something (easier than a lot of other languages, like Java or even Python). – Hejazzman Dec 05 '11 at 21:19
  • 15
    "It’s cocoa that’s mac-native, not Go", actually Go is equally mac-native. The Go compiler is a native OS X executable that produces native OS X executables. Does anybody in this thread know what they are talking about? – Hejazzman Dec 05 '11 at 21:20
  • @porneL any luck on this over the past year or so regarding integration? Seems like using CGo one could build up a pretty attractive package. – ylluminate Jan 25 '14 at 19:20

3 Answers3

11

CGo is what enables you to call C code.

See the CGo doc and the informative, official blog post on it.

There does not seem to be cocoa bindings/libraries yet, but you may want to check out the GTK package for reference.

Kissaki
  • 8,810
  • 5
  • 40
  • 42
  • 3
    [MonoMac](https://github.com/mono/monomac) should also give an idea of how to use C calls to interface with Objective-C – Asgeir Jun 13 '11 at 11:44
9

Right now there doesn't seem to be a package for binding Cocoa to Go. Cocoa is written in Objective-C which is a superset of C. Objective-C messages are (or at least used to be, not sure about the modern compilers) translated to C function calls by the compiler, to something like this:

objc_msgSend(object, sel_getUid("foo:bar:err:"), var, var2, errVar);

So it's definitely possible to use Cocoa from Go.

If you run in to a problem where you find you would like to use Cocoa in a Go app, IMHO take a step back and think about the problem you're trying to solve. Cocoa makes heavy use of named parameters and methods can have quite long signatures. This works well in Objective-C but I doubt the code would look as good in Go. On the other hand, Go solves another set of problems. Maybe writing a library (application logic) in Go and GUI code in Objective-C/Cocoa would do the trick?

TL;DR: How about writing model in Go and GUI code in Objective-C?

Albin Stigo
  • 2,082
  • 2
  • 17
  • 14
  • 6
    This is exactly what I'm considering doing for a project: Cocoa/ObjC for the GUI, writing the main model in Go. How would you do that? I understand there's a cgo library for calling C _from_ Go if it's processed with the Go compiler, but I was hoping there would be a way to, for example, compile executable code with Go and then link to the library within Cocoa in Xcode. Suggestions? – Dean Stamler Aug 02 '13 at 20:51
  • "During the Go 1.4 cycle, GOOS=android will be introduced to the Go repository". It would be awesome if GOOS=ios would be possible, too. So one would be able to link the go code in the GUI Cocoa/ObjC. – mattes Dec 04 '14 at 23:39
2

You can have a look at my blog post as an example. I'm afraid I didn't keep working on it, but here's the source code that can help you setting up a bare Cocoa/ObjC/Go project.

You're gonna be able to do something like this, as mentioned in the README.

package main

import (
  "github.com/alediaferia/gogoa"
)

func main() {
    app := gogoa.SharedApplication()
    window := gogoa.NewWindow(0, 0, 200, 200)
    window.SetTitle("Gogoga!")
    window.MakeKeyAndOrderFront()

    app.Run()
}
alediaferia
  • 2,537
  • 19
  • 22