Hello World
Save this snippet into a file named hello.v
. Now do: v run hello.v
.
That is assuming you have symlinked your V with
v symlink
, as described here. If you haven't yet, you have to type the path to V manually.
Congratulations - you just wrote and executed your first V program!
You can compile a program without execution with v hello.v
.
See v help
for all supported commands.
From the example above, you can see that functions are declared with the fn
keyword.
The return type is specified after the function name.
In this case main
doesn't return anything, so there is no return type.
As in many other languages (such as C, Go, and Rust), main
is the entry point of your program.
println
is one of the few built-in functions.
It prints the value passed to it to standard output.
fn main()
declaration can be skipped in one file programs.
This is useful when writing small programs, "scripts", or just learning the language.
For brevity, fn main()
will be skipped in this tutorial.
This means that a "hello world" program in V is as simple as
[!NOTE] If you do not explicitly use
fn main() {}
, you need to make sure that all your declarations come before any variable assignment statements or top level function calls, since V will consider everything after the first assignment/function call as part of your implicit main function.