The qml package is right now one of the best choices for creating graphic applications under the Go language. Part of the reason why this is true comes from the convenience of QML, a high-level domain-specific language that allows describing visual components, events, animations, and content in general in a succinct and pleasing way. The integration of such a language with Go means having both a good mechanism for describing visual content, and a good platform for doing general development under, which can range from simple data manipulation to involved OpenGL content rendering.
On the practical side, one of the implications of using such a language partnership is that every Go qml application will have some sort of resource content to deal with, carrying the QML logic. Such content may be loaded either from files on disk, or from strings in memory. Loading from a file means the content may be organized in multiple files that directly reference each other without changing the Go application, and may be updated and tested without rebuilding. Loading from a string in memory means the content needs to be self-contained, but results in a standalone binary (linking aside – still depends on Qt libraries).
There’s a well known trick to have both benefits at once, though, and the basic solution has already been made available in multiple packages: have the content on disk, and use an external tool to pack it up into a Go file that is built into the binary when the content is updated. Unfortunately, this trick alone is not enough with the qml package, because the QML engine needs to know what resources are available and where so that the right thing happens when it sees a directory being imported or an image path being referenced.
To solve that problem, the qml package has been enhanced with functionality that leverages the existing Qt resource system to pack content into the binary itself. Rather than using the upstream C++ and XML-based resource compiler, though, a new resource packer was implemented inside the qml package and made available both under a friendly Go API, and as a tool that follows common Go idioms.
The help text for the genqrc tool describes it in detail:
Usage: genqrc [options] <subdir1> [<subdir2> ...] The genqrc tool packs all resource files under the provided subdirectories into a single qrc.go file that may be built into the generated binary. Bundled files may then be loaded by Go or QML code under the URL "qrc:///some/path", where "some/path" matches the original path for the resource file locally. Starting with Go 1.4, this tool may be conveniently run by the "go generate" subcommand by adding a line similar to the following one to any existent .go file in the project (assuming the subdirectories ./code/ and ./images/ exist): //go:generate genqrc code images Then, just run "go generate" to update the qrc.go file. During development, the generated qrc.go can repack the filesystem content at runtime to avoid the process of regenerating the qrc.go file and rebuilding the application to test every minor change made. Runtime repacking is enabled by setting the QRC_REPACK environment variable to 1: export QRC_REPACK=1 This does not update the static content in the qrc.go file, though, so after the changes are performed, genqrc must be run again to update the content that will ship with built binaries.
The tool may be installed via
go get as usual:
go get gopkg.in/qml.v1/cmd/genqrc
and once the qrc.go file has been generated, the main qml file may be
loaded with logic equivalent to:
component, err := engine.LoadFile("qrc:///path/to/file.qml")
The loaded file can in turn reference any other content that was bundled
into the Go binary.
For a better picture, this example demonstrates the use of the tool.
Yay! Finally! Thank you so much
Awesome feature! Really great work!