Beginner

Hello World in V

Your first V program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first V program.

The Code

Create a file named hello.v:

1
2
3
fn main() {
	println('Hello, World!')
}

One line inside main and you have a working program – V keeps things simple.

Understanding the Code

  • fn main() - Defines the main function, the program’s entry point
  • println() - A built-in function that prints a string to standard output with a newline
  • Single quotes - V uses single quotes for strings (the formatter v fmt converts double quotes to single quotes)
  • Curly braces - V uses C-family style syntax with curly braces for blocks
  • No semicolons - Line endings are implicit statement terminators

Running with Docker

The easiest way to try V without installing it locally is with Docker:

1
2
3
4
5
# Pull the V image
docker pull thevlang/vlang:alpine

# Run your program
docker run --rm -v $(pwd):/app -w /app thevlang/vlang:alpine v run hello.v

What’s Happening?

  1. docker pull downloads the V compiler image based on Alpine Linux
  2. docker run mounts your current directory and runs v run hello.v
  3. v run compiles your V source to C, compiles the C to a native binary, and executes it – all in one step

Running Locally

If you have V installed:

1
2
3
4
5
6
# Compile and run in one step
v run hello.v

# Or compile to a binary first
v hello.v
./hello

Install V via:

1
2
3
4
5
6
7
8
9
# From source (recommended)
git clone https://github.com/vlang/v
cd v
make

# macOS with Homebrew
brew install vlang

# Or download pre-built binaries from vlang.io

Expected Output

Hello, World!

Key Concepts

  1. fn main() is the entry point - Every V program starts executing from main
  2. println is built-in - No imports needed for basic I/O
  3. Single quotes for strings - V idiomatically uses single quotes; the formatter enforces this
  4. v run for quick execution - Compiles and runs in one step, ideal for development
  5. Compiles to C - V produces human-readable C code, then uses a C compiler for the final binary

A Slightly Expanded Example

1
2
3
4
5
fn main() {
	name := 'World'
	greeting := 'Hello, ${name}!'
	println(greeting)
}

This demonstrates:

  • := declaration - Declares and initializes a variable (type is inferred)
  • String interpolation - ${} embeds expressions inside strings
  • Immutability - Variables are immutable by default; use mut to make them mutable

Variables and Mutability

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fn main() {
	// Immutable (default)
	language := 'V'

	// Mutable - requires explicit 'mut'
	mut count := 0
	count += 1

	println('${language} count: ${count}')
}

V enforces immutability by default. You must explicitly declare a variable as mut if you intend to change its value.

Common Beginner Notes

No Null Values

V has no null. Optional values use the ?Type syntax:

1
2
3
4
5
6
fn find_user(id int) ?string {
	if id == 1 {
		return 'Alice'
	}
	return none
}

No Unused Variables

The V compiler will not allow unused variables – this keeps code clean and intentional.

Tabs for Indentation

V enforces tabs for indentation. The v fmt tool will automatically convert spaces to tabs.

Running Today

All examples can be run using Docker:

docker pull thevlang/vlang:alpine
Last updated: