Hari's Corner
Humour, comics, tech, law, software, reviews, essays, articles and HOWTOs intermingled with random philosophy now and thenGo - how to use glade files as resources
Filed under:
Tutorials and HOWTOs by
Hari
Posted on Wed, Oct 24, 2018 at 22:40 IST (last updated: Wed, Oct 24, 2018 @ 22:57 IST)
Of late I've been learning the Go programming and I am building a small Resume builder using it. I decided to use GTK for the project and used Glade to design the interface. However, if you want to "cook" the .glade file into the project as a resource and not keep it as a separate file, here are the steps to be followed. It's not blindingly obvious, but reasonably straightforward.
Though there are a number of ways to get static resources into a Go project, I chose go-bindata.
Here are the steps:
- Create a "resources" directory in the main Go project folder.
- Create the glade UI file within the resources directory.
- Install go-bindata (this compiles resources within a folder tree into Go source files)
- After making changes to the resource file(s), run
go-bindata resources/
where resources/ is the name of the resource directory. This will create abindata.go
file. - To load from the Glade resource, use the
gtk.BuilderNew
function and then use thegtk.LoadFromString
function to load the resource (convert the byte array into string using type-casting). Use theAsset
function (generated by the go-bindata tool) to get the resource by the name.
Enjoy! Here's the code snippet which shows how to load from resource (where the original resource file is resumebuilder.glade
inside the resources
folder). This approach can be used for any other kind of resource as well.
ui, err := gtk.BuilderNew()if err !=nil {fmt.Println(err.Error())os.Exit(1)}asset, err2 := Asset("resources/resumebuilder.glade")if err2 !=nil {fmt.Println(err2.Error())os.Exit(1)}ui.AddFromString(string(asset))
Comments closed
The blog owner has closed further commenting on this entry.
No comments yet
There are no comments for this article yet.