If you’ve managed to end up at this article, it’s probably because you either have no idea what Vlang is or want to know more. Either way, it’s simple: Vlang is a programming language. Does that clear it up? (Just kidding.)
To be more specific, Vlang is a statically typed programming language created by Alexander Medvednikov that became open source in July 2019. According to the creator, the name was shortened to V so as to avoid messing up Git history. It is similar to Go, and inspired by Rust, Swift, and Oberon.
In the documentation, V boasts about C-like compilation time (the main backend code compiles to C), less memory allocation, easy learning curve, built-in serialization(transformation of objects to a streams of bytes) and no dependencies upon compilation. V’s safety is also top notch with no null, no global variables, no undefined, and support for bounds checking. By default, it also supports pure functions, immutable variables, and immutable structs.
With the basics covered, we’ll spend the rest of the article talking a little bit more about these claims and where V could be headed in the future.
To follow along, you must have git
, a C compiler like gcc
, and make
installed already; if you do not, go ahead and set those up before we proceed.
To begin with Vlang, it is best to install directly from the source (Github).
If on MacOS, Linux, FreeBSD, etc., run:
git clone https://github.com/vlang/v && cd v && make
On Windows:
git clone https://github.com/vlang/v cd v make
This will self compile and build V on your machine.
Next, to avoid retyping the complete execution path repeatedly, add V to your path as follows.
On MacOS:
sudo ./v symlink
On Windows:
.\v.exe symlink
For our first program, we will use the familiar hello world
:
// hello_world.v fn main() { println('Hello World!') }
Save this file as hello_world.v
and run v run hello_world.v
to get your output.
Remember that V is statically typed, so the types come after the argument name, like so:
fn add(x int, y int) int { return x + y }
Like C++, V does not allow function overloading, which means two functions cannot have same name with different parameters. Limiting function overloading is intended to make code easier to maintain.
In V, functions are also private by default, so other modules cannot use them unless we prepend pub
to them, like so:
// a public function pub fn add() { } // a private function fn sum() { }
Variables in V are assigned with :=
, as in the snippet below:
name := 'Victor' age := 23 large_number := i64(9999999999) println(name) println(age) println(large_number)
They are also immutable; to be able to redeclare/reassign a variable, you will have to prepend mut
to it, like so:
mut age := 23 println(age) age = 24 println(age)
As we now know very well, V is statically typed. Below are its primitive types:
bool string i8 i16 int i64 i128 (soon) byte u16 u32 u64 u128 (soon) rune // represents a Unicode code point f32 f64 any_int, any_float // internal intermediate types of number literals byteptr, voidptr, charptr, size_t // these are mostly used for C interoperability any // similar to C's void* and Go's interface{}
Other data types are:
V supports string interpolation as well:
name := 'Victor' println('Hello, $name!') // Hello, Victor!
Vlang has inbuilt HTTP support for data fetching and JSON decoding. To use them, you will need to import their respective modules:
import http import json fn fetchUsers() { users := http.get('http://jsonplaceholder.typicode.com/users') or { println('failed to fetch users from server') return } }
If you plan on working with JSON API like in the example above, you will need to decode the response from the URL (change the data from a string to an object) before you can handle it.
To do this, you will first need to define a struct for your kind of response, as seen in the example below:
struct User { name string email int } import http import json fn fetchUsers() []User { users := http.get('http://jsonplaceholder.typicode.com/users') users := json.decode([]User, users) or { return []User{} } }
V has a built-in package manager called vpm. To use vpm, run:
v install [package]
Similar to Rust, Vlang uses value types and string buffers for memory management, but does not do garbage collection. Below is an official demo from V:
V’s compile time memory management demo. Running the Ved editor on an 8 MB file with 0 leaks.
Sorry about the background CPU fan noise, it’s an old laptop. Support the development of V: https://github.com/sponsors/medvednikov
Autofree can also be disabled so as to perform manual memory freeing -noautofree
.
V has a simpler, more readable syntax than many other frameworks (like Rust), making it clean and easy to use. Its main philosophy is to do each thing in one way with very few rules.
In my own opinion, V is a cleaner version of Go, with cool features like hot reloading, option/result and mandatory error checks, no null, no global variables and no undefined behavior. V also has the upside of being able to be used in most fields such as web development, game development, systems programming, GUI, embedded, tooling, mobile (WIP), and others.
While there is a lot more work to be done for V to surpass some of the other frameworks out there, now is a great time to get to understand the language and contribute to its development.
With so many other programming languages out there, it is important to remember that V is still in alpha stage, which means most parts are still in development. Nonetheless, it seems like a pretty good language with its “one way of doing things” approach. This makes code predictable and very maintainable.
The V documentation also adds to its simplicity and does a good job helping users get a grasp of what everything is and how it works.
For more about Vlang, check out some of the resources below. In the meantime, I hope this article has answered some of your questions about the V language and its role in modern programming.
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowThe useReducer React Hook is a good alternative to tools like Redux, Recoil, or MobX.
Node.js v22.5.0 introduced a native SQLite module, which is is similar to what other JavaScript runtimes like Deno and Bun already have.
Understanding and supporting pinch, text, and browser zoom significantly enhances the user experience. Let’s explore a few ways to do so.
Playwright is a popular framework for automating and testing web applications across multiple browsers in JavaScript, Python, Java, and C#. […]
6 Replies to "What is Vlang? An introduction"
Great article
Hey can you do a post on the Concurnas programming language? It’s for Concurrent programming on the JVM and it seems to be out of beta
C++ does have function overloading..
Great overview. Noob question, but in your fetchUsers function you’ve defined users a second time without prepending mut to the original declaration. Is that a special syntax case or maybe you had declared that already?
You are doing V a disservice by posting source code that you have clearly never tested! Please do so and fix all broken pieces…
Can you kindly state which source code is broken???