Zig vs Go: init and run
Source: Dev.to
Initialization
In Go we initialize a module with:
go mod init module-name
In Zig the equivalent is simply:
zig init
Zig does not require explicit “module paths” like Go, because there is no direct correspondence between a package import and a URL. This makes the approach more flexible, though less standardized.
Hello World Example
Go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hell world")
}
Zig
const std = @import("std");
pub fn main() !void {
std.debug.print("Hell World", .{});
}
Entry Point
Both languages reserve main as the entry point for an application.
- Neither
mainfunction accepts input parameters; arguments must be handled via standard‑library functions. - Return handling differs: Zig treats
mainlike any other function, returning an error or a value (e.g.,void). In Go, a successful return is implicit, and errors are typically handled insidemainusingos.Exit(1)orpanic("error!").
Running and Building
Running
# Go
go run main.go
# Zig
zig run src/main.zig
Building
# Go
go build main.go
# Zig
zig build src/main.zig
When building with Zig, a zig-out/bin folder is created containing the generated binary. This folder is a convention; examining build.zig shows that the definitions of build, run, and test are not hard‑coded into the tools but are expressed in code, using Zig itself as the language.