Cocoa bindings for the Go language - cocoa

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?

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.

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?

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

Related

Reading windows properties in go

I'm trying to read values from the windows property system, specifically the System.Identity.UniqueID (Ref: https://learn.microsoft.com/en-us/windows/win32/properties/props-system-identity-uniqueid). Is there a way to read this from Go ? either natively, or through C-Go?
The most simple way to read from Property System APIs in Go is to build a dll written in c++, as this requires using COM (and Go definitions for COM are often incomplete/have issues). Microsoft provides a sample project for reading and writing the values to and from files here.
If you want to implement it in pure Go take a look at go-wca and its CaptureSharedEventDriven sample. This library implements the IPropertyStore interface and some of its dependencies in GO although the example is using it to get the device friendly name from the Property Store (you will need to implement the WinAPI calls if you want to read it from files).
The Go project denisbrodbeck/machineid proposes to use machineid in id_windows.g
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Cryptography`, registry.QUERY_VALUE|registry.WOW64_64KEY)
That is because of issue #2
They suggested the assembly: System.Identity.UniqueID
I know of this thread, but I've never managed to get hold of that specific value. Information on google is +very+ sparse.
I've tried to fetch that value with C#, but never found the specific assembly to get that variable from 😕
So System.Identity.UniqueID does not seem easily readable.
You might have to combine other unique Windows properties, as in here.
I have no Windows machine, so I can't provide working example, but I suppose that you can take a look at RegOpenKeyEx
and example of its usage at https://golang.hotexamples.com/examples/syscall/-/RegOpenKeyEx/golang-regopenkeyex-function-examples.html#0x086674b6787cae709f7567bc2c682e747cd85debf2a6db841413704af324bee9-50,,104,

Can VPP plugins be implemented using Go?

VPP provides the I/S for developing custom plugins that can be hooked into a graph of nodes. I've only seen examples for such plugins written in the C language, and was wondering whether other language, Go for instance, can also be used to write such plugins.
I have no idea what "VPP" is but nonetheless the answer is: "maybe"; here's why:
Go code is able to interface with C libraries via its facility known as cgo.
cgo is a multiple-faceted thing: it allows you to "export" certain Go functions in a certain way so that they can be called from the C side, and it allows you to call functions from the C side. It also allows you to write bits of inline C code to provide glue for the C side, when necessary.
Since some time Go building toolset (at least its "reference" implementation) provides for compiling Go code into a static or dynamic library with C-compatible API.
See this.
With these things in mind, in theory, it should be possible to do what you're after.
Note some possible obstacles:
Most of the time, if a "platform" allows you to write a "plugin" in C, it presupposes your plugin will make extensive use of the platform's own API.
This usually means your plugin is supposed to include certain header files provided by the platform.
The platform might also require your plugin to link against some platform-provided library (usually shared), or libraries.
cgo can do all of the above, but you will need to scrutinize the API provided by the platform and maybe write Go helpers to make its usage more natural for the Go code.
Building/linking issues (usually the locations of the header files and the libs) may also be a thing to solve.

Import OSX bundles in golang?

So, in python if I want to access an OSX bundle I can use the objc module like this:
import objc
objc.loadBundle('CoreWLAN',
bundle_path='/System/Library/Frameworks/CoreWLAN.framework',
module_globals=globals())
Is there an elegant way to do this in golang, perhaps with a module? Google is not finding me much. Do I have to call out to this using the C import features of the language? Is this even possible?
My specific situation is that I would like to read WIFI data for available access points, channels, signal strength and signal/noise ratio for talking to the Google Maps Geolocation API.
Andlabs gave an excellent response that he has unfortunately not put in as an answer.
Basically you use cgo like this...
//cgo LDFLAGS: -framework CoreWLAN
...and you are able to call into the framework directly. To quote his responses:
A .framework bundle contains at its top level a dynamically loaded library. In the case of the system frameworks, this dynamically loaded library's filename is the base name of the framework, and does not have the typical .dylib extension. If you are willing to use libdl directly, which means cgo, you can dynamically load this dylib and call its functions directly. But since we're using cgo at this point, there's a better way: //cgo LDFLAGS: -framework CoreWLAN and then use the API directly from cgo. – andlabs
And in fact, looking into it, CoreWLAN's API is Objective-C based, so you have no choice but to use cgo and do the latter of what I said :/ (Do not try to use the Objective-C runtime API directly; that leads to verbose, unsafe, and nonportable code.) – andlabs

In Swift, what are my code reuse options for designing console applications, and how do I implement them?

My current code base involves writing console applications in C++ where I import a static library containing many common classes.
I would like to move to Swift and replicate this process. However, I have come across a couple difficulties:
As it stands, one apparently cannot create static libraries in Swift.
As it stands, one apparently can not import a framework into a Swift console application.
Currently I have been experimenting with writing a single view application and importing Frameworks that way, but enough has gone wrong that I would really like to simplify that and stick with console applications.
Given that, so far it appears that my only option for code reuse is to keep Swift files in common directories and drag them as needed into the console application. Since these console applications are for my own use and I'm only interested in the data they generate (i.e. I don't actually give the program to a user) this is actually a workable solution. I was hoping, however, there might be something a bit cleaner.
Any other suggestions on what to do for code reuse for pure Swift console applications, and if so, how do I go about doing it?
You can create a standard Swift framework that contains your common classes, and then create a console application that lives in a bundle. This will allow you to copy the framework into the bundle's framework folder which your console application will have access to at run time.
You can find more details about this approach in Alsey Coleman Miller's article about using embedded frameworks in OS X command line applications.
The downside, of course, is that your console application now lives inside a bundle.

Is there an equivalent to HLint for Erlang?

HLint is a Haskell lint tool for making code more idiomatic. Is there something like it for Erlang?
There is a tool called "Tidier": http://tidier.softlab.ntua.gr/mediawiki/index.php/Main_Page, which is based on a simpler module called erl_tidy which is part of the syntax_tools library: http://www.erlang.org/doc/man/erl_tidy.html.
Tidier is used via a web interface, letting you choose interactively which changes you want it to perform. It can do some amazing things, and is a great tool for learning how to write idiomatic Erlang.
Yes there is. Its called erl_lint
There's now also elvis. Although it is not specifically a linter, it does check that Erlang code conforms to certain rules which can be configured.

Resources