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:
| |
One line inside main and you have a working program – V keeps things simple.
Understanding the Code
fn main()- Defines themainfunction, the program’s entry pointprintln()- 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 fmtconverts 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:
| |
What’s Happening?
docker pulldownloads the V compiler image based on Alpine Linuxdocker runmounts your current directory and runsv run hello.vv runcompiles 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:
| |
Install V via:
| |
Expected Output
Hello, World!
Key Concepts
fn main()is the entry point - Every V program starts executing frommainprintlnis built-in - No imports needed for basic I/O- Single quotes for strings - V idiomatically uses single quotes; the formatter enforces this
v runfor quick execution - Compiles and runs in one step, ideal for development- Compiles to C - V produces human-readable C code, then uses a C compiler for the final binary
A Slightly Expanded Example
| |
This demonstrates:
:=declaration - Declares and initializes a variable (type is inferred)- String interpolation -
${}embeds expressions inside strings - Immutability - Variables are immutable by default; use
mutto make them mutable
Variables and Mutability
| |
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:
| |
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