Gotk3: GTK3 the Go way
blog.conformal.com
blog.conformal.com
I've personally been using a Qt5.1/QML based solution with a Go/CGO based QML plugin that acts as a bridge between QML and backend Go code. It doesn't try to export much of Qt to Go, it basically expects you to write the UI logic in QML/QtQuick and then just use Go for the underlying app logic, using the QML plugin bridge to allow Go code to call QML functions and vice-versa. In practice this works somewhat like writing a Go server that manages an HTML UI for doing browser-based UIs with a web-based RPC system, except I can write the UI code in QML which I find much preferable to HTML/JavaScript (variable binding Just Works, no need for frameworks like Angular, no need to mess with CSS, can efficiently pass around binary data in byte arrays easily, etc).
Currently my solution isn't even half-baked, I've been implementing it to serve a specific app, and it currently has some dependencies that force it to be Windows only (these could be trivially removed, but doing so isn't important for my app). I may share the code for this sometime in the future after it has matured a bit more, though there's really not that much to it.
In C++, when you define a class, you define the constructor(s) and the destructor. Then the destructor is called whenever the object goes out of scope. Easy. This is the gist of RAII. Furthermore, in C++, a default destructor is automatically generated for you, which will ensure all the objects members are destructed with your object,[2] so you can avoid even worrying about the destructor.
Yeah, it's not as simple as GC, but it seems like a great compromise between manual memory management and garbage collection.
[1]: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initial...
[2]: Provided no members are dynamically allocated. If you allocate an array in your constructor, for example, you need to free it in the destructor. You need to define a destructor to free dynamically-allocated memory, or to do other cleanup.
Probably the reason that more languages don't use RAII for memory management is safety. C++, even with RAII, is not safe—you can violate memory safety with certain uses of references and iterators. Rust has to go to great lengths (lifetimes and the borrow check—things that have only been in the research landscape) to make this safe.
Unfortunately for me, the rust compiler is written in rust, so I would have to adventure to grab a Linux and try my hand at a cross compile before I can play with it on my OpenBSD box.
Edit: To be clear it's the latter question, but more general. C++'s implementation has many opportunities for errors, but I do not think the technique is inherently unsafe. (PL research has proven many similar systems based on unique types and borrowing to be type- and memory-safe.)
If you're talking about C++'s unsafe implementation of RAII, that's true of course.
Perhaps the real answer to "Why isn't RAII more popular?" is that C++ has poisoned the well for many programmers.
No, that would be unsound.
> Perhaps the real answer to "Why isn't RAII more popular?" is that C++ has poisoned the well for many programmers.
Totally agreed. (But that said, I think that Go, and most other languages, made the right decision when going with pervasive GC. It makes the language much simpler, both in terms of implementation and interface. It costs some performance, of course, but being as fast as C in all cases was never a design goal of Go, according to the FAQ. The complexity, both in interface and implementation, that languages buy into if they want safe manual RAII-style memory management is not trivial.)
People use shared pointers to mitigate that problem, but shared pointers are (a) a reference counting scheme with all the attendant problems and (b) not compatible with code that holds references but doesn't speak the same shared pointer scheme.
(It's been a bunch of years for me since I wrote C++ professionally; that may show here.)
https://qt-project.org/doc/qt-5.0/qtcore/qmutexlocker.html#d...
kbarrett@vernon-linux:~/go-work/gotk$ go version
go version go1.0.2
kbarrett@vernon-linux:~/go-work/gotk$ go build
# github.com/conformal/gotk3/glib
glib.go:380[/tmp/go-build210667265/github.com/conformal/gotk3/glib/_obj/glib.cgo1.go:370]: function ends without a return statement
glib.go:743[/tmp/go-build210667265/github.com/conformal/gotk3/glib/_obj/glib.cgo1.go:741]: function ends without a return statement
function ends without a return statement. If this is a problem with my version of go, I will try and fetch the latest head from git. I am interested!sudo go get github.com/conformal/gotk3/gtk -- gives the same error.
I'm going to try it on FreeBSD.
hg clone http://code.google.com/p/go
cd go/src
./make.bash
(or ./all.bash if you want to run all the unit tests)Same holds true even on Windows if you have a local gcc like MinGW (the only notable difference is you'll execute make.bat instead of make.bash if you're in a Windows cmd shell instead of bash).
From poking around, still a pipe dream for the moment.
Ideally the GTK main loop would run in its own OS thread, and there would need to be a way to ensure that no GTK calls happen in other goroutines. Other (threaded) environments tend to handle this by deferring via a one-off "idle" function that is scheduled and run by the GTK main loop for things like that.
1. Standard C++ ui designed in a form designer and connected to C++ classes.
2. QML + javascript.
3. OpenGL with the ability to use widgets on the OpenGL viewport.
I'd like to see a C based toolkit, that's made specifically for wrapping it up for other languages (in C++, Ruby, Python, Go etc) and has niceties to help with this.
This is exactly, why GTK+ and essentially the whole GNOME stack is now introspected with GObjectIntrospection. So, there you have it.
They also set finalizers.
I just gave it a try now, but the code will not compile on my dev box (OpenBSD, go tip). Even after making some minor fixes (I fixed an #include directive, removed a call to a deprecated function, etc.) there appear to be some serious issues (one error I got mentioned the size of an array being negative).
It appears this code has not been updated in a year, and we wished to target a recent GTK version (3.8, to be precise) with our bindings. Had we known about the project, we probably would have considered submitted patches to fix its issue and bring it up to date with newer dependencies, but even then, I feel that it would be almost as much or perhaps even more to fix and verify an unfamiliar and broken code base than rolling our own.
But I'm glad to see this project seems to be doing memory management in a Go-like manner.
It is a little frustrating as it is one of the first projects that comes up when searching for "go" and "gtk3" on Google. So, it is is an alternative to what you are doing I guess.
So this was just me not being aware of it, but others of us at Conformal were, and due to the above issues we decided to do our own.
However, even ignoring the CGO-based frameworks, there's nothing wrong with mostly using Go as if it were C with closures (and garbage collection, and interfaces, and optional implicit typing and reflection), that's how most Go code looks, even in the standard library. Channels/CSP are great if you need to easily pass data among concurrent goroutines, but not all code neatly fits into that model nor benefits from concurrent execution.
One of the early mistakes most newbie Go programmers make (and I did this myself as well) is overusing channels, just because "hey, channels are super cool"!