02 - Hello World

Every language tutorial starts here. Save this as hello.v:

import std.io

main: f() {
	io.writeln("Hello, World!")
}

Run it with:

verge run hello.v

and you get:

Hello, World!

What every line is doing

That tiny program already shows off most of Verge’s ground rules, so it’s worth slowing down for a moment.

  • import std.io pulls in the standard I/O module. Verge has no implicit preamble — there is no magic namespace that’s always in scope. If you want to write to stdout, you ask for std.io explicitly.
  • main: f() { ... } is the program’s entry point. Every runnable Verge program needs exactly one main. The name: f(...) shape is how all functions are declared, which we’ll come back to — main is not special syntax, just a specially-named function.
  • io.writeln(...) calls the writeln function from the module you imported under the name io. Standard-library calls are qualified with the module name; there’s no unqualified writeln floating around. writeln writes its argument followed by a newline; its sibling io.write leaves the newline off.
  • Braces define blocks. Verge is not indentation-sensitive. The { } decides where the block begins and ends; the tabs inside are convention, there to keep the structure clear.

The short form

A function whose body is a single expression can drop the braces and use = instead:

import std.io

main: f() = io.writeln("Hello, World!")

This is the same program. The = expression form is available anywhere a function body is just one expression, and you’ll see it a lot for small helpers. When you need more than one step, switch back to a braced block:

import std.io

main: f() {
	message := "Hello, World!"
	io.writeln(message)
}

Here message := "Hello, World!" declares a local and infers its type (string) from the value. The := operator is Verge’s “declare and infer” — taken from Go.

Now make it yours

io.writeln takes more than one argument and stitches them together, so you can mix text and values without building the string by hand:

import std.io

main: f() {
	name := "world"
	io.writeln("Hello, ", name, "!")
}
Hello, world!