Getting started with ZIG programming Language
Zig is a low level programming language written by Andrew Kelley that focuses on robustness, optimality and reusability
![Getting started with ZIG programming Language](/content/images/size/w1200/2023/01/zig-lag.jpg)
Zig is a low-level programming language written by Andrew Kelley that focuses on robustness, optimality, and reusability. Most notably the zig compiler is self-hosted. This implies that the zig compiler is written in zig. Zig is a simple language designed without hidden control flow or hidden memory allocations. It also lacks macros and a preprocessor. This guarantees code runs as expected. In terms of speed, Zigs competes with C instead of depending on C.
Zig does not have a garbage collector. Memory allocation in zig is done manually. It is the developer's task to free memory and handle memory errors.
The simplest way to install ZIG
The simplest way to install zig is on Linux is by using snap
, on windows, you can install command line package managers such as chocolatey
and scoop
, on mac, you can install zig using homebrew
to install ZIG.
How to Install ZIG on Linux using snap.
snap install zig --classic --edge
How to Install ZIG on Windows.
Option A: Using chocolatey.
choco install zig
Option B: Using Scoop.
scoop install zig
scoop
Installing ZIG on Mac OS using homebrew.
brew install zig
If the zig
command is not working on your terminal you can locate the zig executable from bin
or wherever it was placed during installation. If you installed ZIG using snap the zig
executable is located in /snap/bin/zig
and can be run directly in the terminal.
╭─alexander@Virtual-Machine ~/Dev/Zig
╰─$ /snap/bin/zig --help 1
Usage: zig [command] [options]
Commands:
build Build project from build.zig
init-exe Initialize a `zig build` application in the cwd
init-lib Initialize a `zig build` library in the cwd
ast-check Look for simple compile errors in any set of files
build-exe Create executable from source or object files
build-lib Create library from source or object files
build-obj Create object from source or object files
fmt Reformat Zig source into canonical form
run Create executable and run immediately
test Create and run a test build
translate-c Convert C code to Zig code
ar Use Zig as a drop-in archiver
cc Use Zig as a drop-in C compiler
c++ Use Zig as a drop-in C++ compiler
dlltool Use Zig as a drop-in dlltool.exe
lib Use Zig as a drop-in lib.exe
ranlib Use Zig as a drop-in ranlib
objcopy Use Zig as a drop-in objcopy
env Print lib path, std path, cache directory, and version
help Print this help and exit
libc Display native libc paths file or validate one
targets List available compilation targets
version Print version number and exit
zen Print Zen of Zig and exit
General Options:
-h, --help Print command-specific usage
If the above guides don't get you set up with ZIG use the below link to install ZIG for your system.
A simple "hello world" in ZIG
Zig files should be saved with an extension of .zig
.
A simple hello world in zig would look like below:
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
try stdout.writeAll("Hello World\n");
}
hello.zig
Some of the zig syntaxes are borrowed from modern commonly used programming languages. i.e. const
to define a constant popularly used in JavaScript.
To simply run ZIG we use the zig run
which usually creates an executable and runs it immediately.
/snap/bin/zig run hello.zig
A ZIG "hello world" can also be written as below using functions:
const std = @import("std");
const stdout = std.io.getStdOut().writer();
fn hello() !void {
try stdout.writeAll("Hello World\n");
}
pub fn main() !void {
try hello();
}
Should you use ZIG in production
I would advise against using ZIG in full-scale production at the moment until the language is fully mature. Things are constantly changing and you might end up with unusable code that spits errors.
Conclusion
Zig advertises itself as a modern C. Its main goal is to compete with C. Zig also allows easy inclusion of C and C++ code in your codebase by shipping with C and C++ compilers. You can even run C and C++ code with ZIG.