site  contact  subhomenews

The Zig language

October 21, 2024 — BarryK

Back in 2022 when I was looking around for alternatives to BaCon, did briefly look at Zig, but decided on Nim. Here are introductions to BaCon and Nim:

"The BaCon programming language"
https://easyos.org/dev/bacon/index.html

"The Nim language"
https://easyos.org/dev/nim/index.html

Last couple of days, took another look at Zig; it has grown enormously in popularity since 2022. Why?...

Zig is a relativitely young language, started by Andrew Kelley in 2015. It is a complete compiler, generating binary executables, unlike BaCon and Nim that generate C code and then require a C compiler such as gcc to generate the binaries. I've been "kicking the tyres" of Zig...

I compiled the traditional "Hello World" CLI program, and it is only 3KB. Furthermore, that binary is statically-linked. First impression, that is incredible.

But it gets better; out of the box, the Zig compiler cross-compiles. You can choose any target you want, x86_64 or aarch64 for example. Furthermore, you can specify specific CPUs. I specified the Intel "nocona" CPU for my "Hello World".

Right off, I see why this will appeal to people who code for embedded systems.

The Wikipedia has an overview:

https://en.wikipedia.org/wiki/Zig_(programming_language)

The Zig website:

https://ziglang.org/

img1

There are some great getting-started websites:

"Introduction to Zig"
https://pedropark99.github.io/zig-book/
...online, also available as an ebook or printed.

"Zig Guide"
https://zig.guide/

"Getting started with the Zig programming labguage"
https://blog.logrocket.com/getting-started-zig-programming-language/

I have a particular like for associative arrays, and yes, Zig has them in the standard-library; they are called "hash tables". I also want the language to support regular expressions; it seems need an external library for that.

I have a need to create small statically-linked binaries, for use in the initrd. But they are useful anywhere, as no reliance on glibc or any libc library. Developers will have encountered this problem, when linked against a particular version of glibc, the binary won't work with a different version of glibc. Yes, you can statically-link with glibc when compiling, but the binary is enormous. So, I have used musl. Up until now.

Another thing I like is to write little programs with GUI, that is, Graphical User Interface. Most commonly I do this with shell scripting and one of the GUI tools, such as gtkdialog, xdialog and xmessage. BaCon and Nim also have GUI toolkits; I have used HUG with BaCon.

What about GUI toolkits for Zig? There is a website with links to all-things-Zig:

"Awesome Zig"
https://github.com/zigcc/awesome-zig

I like the look of "capy":

https://capy-ui.org/

https://github.com/capy-ui/capy

img2

Here is another GUI toolkit, not in the Awesome-Zig list:

https://github.com/desttinghim/zig-libui-ng

...awhile back, in 2022, did look at the Nim binding for libui-ng and liked its simplicity.

Finally, some Zig reviews:

From the perspective of a Rust programmer:
https://blog.lohr.dev/after-a-day-of-programming-in-zig

This looks like a sales-pitch:
https://leaddev.com/tech/why-zig-one-hottest-programming-languages-learn

From the perspective of a Go programmer:
https://medium.com/@vladimirvivien/things-i-like-about-zig-as-a-go-programmer-75eb02aab00f

Looks good. The intention is that once Easy Daedalus 6.4 is released, will take some time out from Easy development and play with Zig. As an exercise, might rewrite 'debdb2pupdb' in Zig, see how it compares with the Nim code.

EDIT:
Here is another Zig tutorial, very recent:

"Programming with Zig: from basics to mastery"
https://gencmurat.com/en/pages/programming-with-zig/
   

Tags: linux