Insight into the latest and most valuable projects: This issue introduces the Zig programming language
brief introduction
Zig is a modern programming general purpose programming language and a strong competitor for C.
Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Home page address: https://ziglang.org/
Chinese Home Page: https://ziglang.org/zh/
Characteristic
- Compact and concise language
- Compile-time code execution
- Maintain Code with Zig
More can be viewed directly from the description on the homepage above.
Clone Source
zig can be installed in several ways: https://ziglang.org/zh/learn/getting-started/
zig hasn't released 1.0 yet, so building directly from source code is worth trying.
Clone Source:
git clone https://github.com/ziglang/zig.git
Or clone from a mirror on gitcode
git clone https://gitcode.net/mirrors/ziglang/zig.git
Compile
Environmental requirements
- cmake >= 2.8.12
- GCC >= 7.0.0 or clang >= 6.0.0
Compile command:
- mkdir build
- cd build
- cmake ..
- make install
If you are on a Mac system, you need to ensure that
- Install llvm using brew install llvm
- Use cmake.. - DCMAKE_ PREFIX_ PATH=$(brew--prefix llvm) -DCMAKE_ INSTALL_ PREFIX=/usr/local command compilation
View zig command options
Mastering a programming language is largely a process of mastering the tool chain of that programming language. The use of the zig programming language starts with mastering the zig command itself. Enter zig directly into the console and return to see the option information:
info: 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 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 Debug Commands: changelist Compute mappings from old ZIR to new ZIR error: expected command argument
Notice that on the last line, error: expected command argument prompt requires a command parameter after the Zig command, so Zig prints the option help information without us. If you use zig-h or zig-help directly, it is the usual way to view option help.
Zen of zig
Each programming language has its own design philosophy, and many languages use zen of xxx to represent the philosophy of that language. Here zen means zen, which means wisdom after deep thought.
For example, the python language also has a page on the language document page:
PEP 20 – The Zen of Python: https://www.python.org/dev/peps/pep-0020/
For example, someone wrote a page for the C++ programming language:
The Zen Of C++: https://www.experts-exchange.com/articles/10157/The-Zen-Of-C.html
For example, the Unix operating system also has its philosophy:
Basics of the Unix Philosophy: https://homepage.cs.uri.edu/~thenry/resources/unix_art/ch01s06.html
The zig programming language has its design philosophy built directly into the command line, using the following commands
zig zen
As you can see, zig's design philosophy is:
* Communicate intent precisely. * Edge cases matter. * Favor reading code over writing code. * Only one obvious way to do things. * Runtime crashes are better than bugs. * Compile errors are better than runtime crashes. * Incremental improvements. * Avoid local maximums. * Reduce the amount one must remember. * Focus on code rather than style. * Resource allocation may fail; resource deallocation must succeed. * Memory is a resource. * Together we serve the users.
Programmers can derive inspiration from these entries, and even if zig is not used, good philosophies are important in other development contexts.
View zig version number
You can view the zig version number directly with the command:
zig version
The results are as follows:
0.10.0-dev.1058+eaf1c97ce
Viewing environment variables for zig
Command:
zig env
You can see that zig's environment variables are very human (Mac systems):
{ "zig_exe": "/usr/local/bin/zig", "lib_dir": "/usr/local/bin/lib/zig", "std_dir": "/usr/local/bin/lib/zig/std", "global_cache_dir": "/Users/username/.cache/zig", "version": "0.10.0-dev.1058+eaf1c97ce" }
This information is listed
- Zig_ Exe: The path to the Zig command
- Lib_ Dir: zig's lib path
- Std_ Dir: The location of the Zig standard library, as you can see, is lib_ A subdirectory of dir
- global_cache_dir: Cache directory
- Version:version number
As you can see from here, zig's command system resembles git's and uses command action mode, which is more friendly and modern.
Create a zig command line program
Using the zig command, you can quickly create a command line program, create a test directory, and execute the command in that directory
zig init-exe
The resulting directory structure is as follows:
. ├── build.zig └── src └── main.zig
Where the src/directory is the source directory and build.zig is the "script" for building a project, and it is also a zig program itself, which brings strong consistency: source and source build scripts use a language. Look at src/main.zig's hello world code:
const std = @import("std"); pub fn main() anyerror!void { std.log.info("All your codebase are belong to us.", .{}); } test "basic test" { try std.testing.expectEqual(10, 3 + 7); }
The source code is explained as follows:
- The first line imports the standard library using @import("std").
- Next comes the program entry main function, in which a line of information is printed using the standard library.
- Next is a unit test program
Build zig program
The zig command line makes it easy to build:
zig build
The command line of the build process is very clean. Instead of displaying the full screen build information, the information lines are refreshed in place. The directory structure after the build is complete is as follows:
. ├── build.zig ├── src │ └── main.zig ├── zig-cache │ ├── h │ │ ├── 237f592bfc87d556dffba90a015637a1.txt │ │ ├── ae3ffb1f119a3161a4cc2f48480dfa9d.txt │ │ └── timestamp │ ├── o │ │ ├── 23650d5ec66547ed61e5e4072ce70ba7 │ │ │ ├── build │ │ │ └── build.o │ │ ├── 99b9e6b82f4011e2eb15a72ca8fbb5f3 │ │ │ ├── test │ │ │ └── test.o │ │ ├── b91f14aec3e67ae9026c15e937a2e811 │ │ │ └── builtin.zig │ │ └── c8809ea18e0907e87e2698d775132309 │ │ └── builtin.zig │ ├── tmp │ └── z │ ├── c8d7e1e8c507700e93104fdc5bbd7fcd │ └── dd90fc6dc4731d7ca192082b0db949aa └── zig-out └── bin └── test
The zig-cache directory is the intermediate directory for program building, while zig-out is the output directory for program building. You can see that a test program is generated in the bin directory.
Run zig program
The resulting binary program can be run directly:
./zig-out/bin/test
The program output a line of information:
info: All your codebase are belong to us.
Run directly from source
Alternatively, you can use the run command to run the target source directly
zig run src/main.zig
You can see that zig compiles and runs the program directly as follows:
info: All your codebase are belong to us.
Execute unit tests
In src/main. There is a unit test code in zig:
test "basic test" { try std.testing.expectEqual(10, 3 + 7); }
In fact, zig supports direct command execution of tests on source code containing unit tests:
zig test src/main.zig
You can see the results:
All 1 tests passed.
Master modern programming languages
Generally speaking, zig is a new modern programming language. From the zig command line, the whole design is very simple and easy to use. One of its core designs is to reduce magic:
- No implicit control flow
- No implicit memory allocation
- No preprocessor, no macros
According to one statistic, engineers spend an average of 70% of their time reading code. One of the pain points in reading code is that it is full of "hidden magic". Zig's design is valuable in this respect. Different programming languages represent different target development environments and platforms, and Zig is a strong competitor for C. To master modern programming languages, you can start by writing some command line applets for daily processing using zig.
–end–