Saturday, April 19, 2025

learning Zig programming language

Zig is a relatively new programming language
intended to be a "modern version of C": low-level "systems" language
for code where performance and safety are important.

While both Rust and Go have similar objective,  there are some important differences. 
Zig, in fact, is the closest to "spirit" of C:
relatively simple syntax, full control and responsibility for memory management.

Rust is much bigger / more complex, but also more used. 
Almost like C++ is to C, Rust may be to Zig. 

Go, on the other side is also simple, fast, but it does have "GC", automated memory management.
That makes it much easier to use correctly, while limits its usage for real-time critical applications.

At this time, there are not many prominent projects using Zig yet. Rust is more popular.
I

Zig (programming language) - Wikipedia

Home ⚡ Zig Programming Language

Learn ⚡ Zig Programming Language

Getting Started ⚡ Zig Programming Language

Samples ⚡ Zig Programming Language

const std = @import("std"); pub fn main() !void { const stdout = std.io.getStdOut().writer(); try stdout.print("hello world!\n", .{}); }

$ zig run hello-world.zig

or

$ zig build-exe hello-world.zig $ ./hello-world hello world!


Here is a fun demo of Windows GUI low-level program, fully functional!
Adding more GUI elements quickly gets much more verbose, but resulting EXE is small and fast.

// https://ziglang.org/learn/samples/
const win = @import("std").os.windows;

extern "user32" fn MessageBoxA(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32;

pub fn main() !void {
    _ = MessageBoxA(null, "world!", "Hello", 0);
}


What is the EASIEST way to learn "zig"? : r/Zig


e-books:


courses:



Zig or Go, but not Rust (ThePrimeTime)

Go GUI


there are several Go packages that wrap the Windows GDI functions in a more idiomatic and less verbose interface. Here are some of the better options for creating a chess GUI:

  • Fyne: A cross-platform GUI toolkit that works on Windows and abstracts away the low-level details:
go get fyne.io/fyne/v2
fyne-io/fyne: Cross platform GUI toolkit in Go inspired by Material Design @GitHub

  • Walk: A Windows-specific GUI toolkit for Go that provides native Windows GUI capabilities:
go get github.com/lxn/walk
lxn/walk: A Windows GUI toolkit for the Go Programming Language @GitHub

  • Gio: A cross-platform GUI library with immediate mode rendering:
go get gioui.org
Gio UI

  • Ebiten: More game-focused but excellent for something like chess:
go get github.com/hajimehoshi/ebiten/v2





even without packages, Go can access Windows (and other OS) GUI features
Here is a super-simple but functional Go GUI "Hello World" program.

package main

import (
    "syscall"
    "unsafe"
)

var (
    user32      = syscall.NewLazyDLL("user32.dll") // Loads the user32.dll library dynamically
    messageBoxA = user32.NewProc("MessageBoxA")    // Gets a reference to the MessageBoxA function
)

func main() {
    messageBoxA.Call(
        0, // NULL for the parent window handle
        uintptr(unsafe.Pointer(syscall.StringBytePtr("world!"))), // message text
        uintptr(unsafe.Pointer(syscall.StringBytePtr("Hello"))),  // title
        0, // type of message box, default
    )
}