Introducing the V Tutorial!
Introducing the V Tutorial!
A few months ago, I've seen a project in my GitHub Trending called V. I have seen some cool things about this language, and I think that V is a very promising language! So today, I introduce you, the V tutorial! :)
But first, what's V? 🤔
V is, as it introduced:
Is a statically typed, compiled, small, and fast language, for developing maintainable software. The syntax of V is similar to Go and has been influenced by Oberon, Rust, Swift, Kotlin, and Python.
Some cool things about V 🎉
- Simple: V is similar to Go, so if you mastered Golang, then you know at least 80% of V (at least the devs of V promises that)
- Safety: There is no null, no global variables, no variable shadowing, immutable variables by default, no undefined behavior, and many more!
- Performance: V is as fast as C (V's main backend compiles to human-readable C, and again, the developers promises for this, not me).
- Plays well with C: V can translate your C project in >1s! I don't know what this is for, but if you are a C dev and wondered to move your C projects into V, well, this is useful for you!
Here is a taste of it! 😉
Looks similar, right?
Now, we will go to the tutorial!
In this, I will mostly take them from the V official document and present it in a better and friendlier way (I hope), also I will reduce some stuff from the official to keep this simple. Mostly the example code are just a modified version of the code from the official doc, some are made by me (Disclaimer: Some of them can contain errors, so I would not recommend you to copy it like a zombie). If you want to learn more about V, please check out the official V documentation
Now, let's get into in!
First, we need to install the V compiler first, so we can run our V programs! We will build it from the V source code.
When you're done, we will start building V by cloning their GitHub repo:
And that's it! Now you have a V executable at
[path to V repo]/v, and
[path to V repo] can be anywhere.
Now you can try your V compiler using
./v run examples/hello_world.v
Symlinking to saving time
I highly recommended that you put your V compiler in the PATH, it saves a lot of time. V has the
symlink command to make it more convenient and easier to do it.
After that, you should close your shell/editor, so V can pick the new PATH variable.
Install the V compiler through an AUR helper
If you are a Manjaro user (like me), luckily, you can install the V compiler already with no need to build though an AUR helper, like
We will start with the modified version of the traditional
Hello world! program,
In the code, we create the entry point function (aka the
main function) that will run any code inside it, in that function, we use the built-in function
println() to print a string (In this case is
Now, we can run our program by saving it to a file called
hi_v.v and run it using
v run hi_v.v. You should see the phrase
Hello V! shows when it's done.
A cool thing about V is that you can skip the
fn main() declaration, this is useful when you want to write small programs, or learning the language, for clarity, the
fn main() declaration will be skipped in this tutorial.
That means that you can just type:
Like Go, the type comes after the function name and the argument's name (In case here is
int, aka integers). And like Go and C, functions can't be overloaded, simplifies the code and improve maintainability and readability. Functions can be used before or after their declaration: The function
add are declared after the entry point function, but can still be called from it. This is true for all declarations in V and eliminates the need for header files or thinking about the order of files and declarations like what you will deal on C++ and C.
Returning multiple values
You can return more than 1 value from a function in V!
Accept multiple arguments
You can assign multiple arguments to a function, like Python's
Variables are declared and initialized using
:=, and this also the only way to declare and initialize a variable in V, means that the variable always have an initial value, and the variable's type is inferred by the value from the right side. If you want to change the type to another, use type conversion:
T(v) changes the value
v to type
T, for example:
int(10.2) will convert the float
10.2 to integer.
Unlike many languages, V only allows you to declare variables as functions, global variables are not allowed. Means that:
All the variable and function names must be use the
snake_case style to name them (e.g.
hello_world). And with the type names is
In V, all the variables are immutable by default, to be able to change their value, you need to use
mut. And to give a mutable variable a new value, you need to use
Strings in V are a read-only array of bytes, and they are encoded using UTF-8. They are immutable and can't be changed.
Like in C and Python, V also has escape characters, like
\t and so on.
If you want raw strings, use
r, escape characters is useless to raw string.
String Interpolation (Embedding variables and stuff to strings)
String interpolation is simple in V - use
$ after variable name, the variable will converted to string type and embedded to the string.
If you want to do more complex expressions, use
An array is a type that contains some element with the same type, the type of the array is determined by the first element of that array, for example,
[1, 2, 3] is an array of integers, because the first element,
1 is an integer. And they are immutable by default.
Array operators, methods, and other things with it
To import modules in V, you use
You can also import the module name as another name to save time.
Statements and expressions
if statements are straightforward as in many languages, there is no parenthesis surrounding the condition like in C, and the braces is always required.
if can also be used as an expression, like this:
There is only one looping keyword in V:
for . There are several styles for this loop.
Like Python, V also has the
for value in arr form. It's used to, well, you guess it, going through each element in the array. And when you also need their index, then use
for index, value in arr instead.
And because the value is read-only, so when you need to change their value, you have to use indexing.
0..5 makes an exclusive range, means that they represent values from
4, not including
Looping though a map value
For with condition
If you remove all the conditions for the
for loop, it will become an infinite loop, never stop.
If you don't like the modern loop style or just want your code to feel more oldschool and friendly, you can use the good old C style, it's safer than the
while one, because it's easy to forget to update your values, resulting in an infinite loop.
match is another and shorter way to write a
if - else statement. When a matching branch is found, it will do the code in the following statement block. The
else is used when there is no matching branch. And
match can be used as an expression just like
Decoding JSON in V
JSON is a very popular data format. Every time you need to config your workspace in VS Code, every time you need to make a config file, for your program, so the user can customize it easily, JSON is always the first. And luckily, V has supported a built-in JSON library for you!
println() is not the only builtin function in V, there are some others builtin functions, here's a complete list:
Ok, so after this tutorial, you have learned about the V programming language, you have learned from the basics to some higher concept of it, you also learned about how to decode JSON is V (as a bonus thing). If you have any question, comment down below! 😉
Happy (Late) New Year everyone! :D