Chapter 1 First program
1
Chapter 1

First program

This chapter starts with one Silk source file. You will make the package boundary visible, import the I/O function you need, add a small test, and run the same feedback loop used by larger packages.

Create a workspace

Make a small directory with a source folder and a build output folder.

Terminal
mkdir first-silk
cd first-silk
mkdir src build

Add a Silk source file

The first line declares the package namespace. The import block says where println comes from before the program body starts.

src/main.slk
package first;

import { println } from "std/io";
import { expect_equal } from "std/test";

fn answer () -> int {
  return 40 + 2;
}

fn main () -> int {
  println("hello from silk: {d}", answer());
  return 0;
}

test "answer is stable" {
  expect_equal(42, answer());
}

Check, test, and build

silk check validates the module set without writing an artifact. silk test runs language-level tests. silk build emits the executable.

Terminal
silk check src/main.slk
silk test src/main.slk
silk build src/main.slk -o build/first

The important habit is the loop: check the module set quickly, run focused tests, then build the artifact only after the source boundary is clear.

What changed

  • package first; gave the file a namespace.
  • import { println } from "std/io"; made the stdlib dependency explicit.
  • The test lives beside the code it protects and runs with silk test.
  • The output path is explicit, so build artifacts stay out of source.

Chapter complete

Next, use the same source model with more language constructs.

Continue to the language tour