Skip to content

Latest commit

 

History

History
145 lines (111 loc) · 5.02 KB

File metadata and controls

145 lines (111 loc) · 5.02 KB

So by example

A hands-on introduction to Solod (So) — a subset of Go that translates to C.

This repo contains small examples that show individual language, concurrency, stdlib, and safety features, as well as sample apps and C libraries interop that demonstrate how everything fits together.

Language

Hello world • Values • Variables • Constants • For • If/else • Switch • Arrays • Slices • Maps • Functions • Multiple returns • Variadic functions • For-range • Pointers • Strings and runes • Structs • Methods • Interfaces • Enums • Errors • Panic • Defer

Concurrency

Threads • Channels • Channel synchronization • Channel buffering • Channel timeout • Non-blocking channels • Closing channels • Worker pools • Worker pools w/channels • Mutexes • Atomic counters

Standard library

Memory • C interop • Slices • Maps • Strings • Time • Unix time • Time format • Random numbers • Number parsing • Sorting • Custom sort • JSON • Reading files • Writing files • Scanning • File paths • Directories • Temp files • Testing • Benchmarking Command line • Env variables • Logging • TCP server • Exit

Safety

Dangling pointer • Divide-by-zero • Memory leak • Nil dereference • Panic mode

Apps

Coreutils: cat, cut, head, sort, uniq, wc

Count word frequencies by serge-hulne

DuckDB shell by Lorenzo Mangani

FreeSWITCH module by seven1240

IP address encryption and obfuscation by Frank Denis

Reverse lines in file

TCP echo server and client

C libraries

Bouncing ball animation with Raylib.

Hashing with Sodium.

HTTP calls with curl.

Key-value store backed by SQLite.

TCP echo server using libuv.

Running the code

Install the latest versions of the So compiler and standard library:

go install solod.dev/cmd/so@706286e46712
go get solod.dev@706286e46712

Run a specific example locally with the so run command:

so run lang/05-for
so run apps/head -n 4 data/jenny.txt

You'll need to have a C compiler installed and available as cc, or you can set a custom compiler by using the CC environment variable.

To see the generated C code, use the so translate command:

so translate -o lang/05-for/generated lang/05-for

Partially based on Go by Example by Mark McGranaghan, licensed under CC BY 3.0.