V Programming Language Examples

Examples showcasing the V programming language syntax and usage.

languages
vprogrammingexamples

V Programming Language Examples

The V programming language is a simple, fast, safe, and compiled language built for performance and maintainability. Below are examples that demonstrate key features of the V language.

Hello World

// A simple program to print "Hello, World!"
fn main() {
    println("Hello, World!")
}

Variables and Constants

// Declare immutable variables (constants)
const pi = 3.14159

// Declare mutable variables
mut age := 30
println("Age before: $age")

// Update variable value
age = 31
println("Age after: $age")

Functions

// Define a function that adds two numbers
fn add(a int, b int) int {
    return a + b
}

// Call the function
fn main() {
    sum := add(5, 10)
    println("Sum: $sum")
}

Working with Arrays

// Create and manipulate an array
fn main() {
    numbers := [1, 2, 3, 4, 5] // Initialize an array
    mut squared := []int{}    // Mutable array for squares

    // Square each number
    for num in numbers {
        squared << num * num
    }

    println("Original: $numbers")
    println("Squared: $squared")
}

For Loop

// Demonstrating a for loop
fn main() {
    for i := 0; i < 5; i++ {
        println("Iteration: $i")
    }
}

If-Else Condition

// Demonstrates the use of if-else statements in V
fn main() {
    age := 18
    if age >= 18 {
        println("You are allowed to vote.")
    } else {
        println("You are not allowed to vote.")
    }
}

Reading User Input

// Read user input and process it
import os

fn main() {
    name := os.input("Enter your name: ")
    println("Hello, $name!")
}

String Manipulation

// Working with strings in V
fn main() {
    name := "Syntax Recall"
    println(name.len)          // Length of the string
    println(name.to_upper())   // Convert to uppercase
    println(name.to_lower())   // Convert to lowercase

    // String interpolation
    println("Hello, $name!")
}

Maps

// Demonstrating map usage in V
fn main() {
    mut students := map[string]int{
        "Alice": 85,
        "Bob": 90,
    }

    students["Charlie"] = 78 // Add new entry
    println(students)

    score := students["Alice"] // Access value
    println("Alice's score: $score")
}

Structs

// Define and use a struct
struct User {
    name string
    age  int
}

fn main() {
    user := User{
        name: "Alice",
        age: 25,
    }

    println(user.name) // Access properties
    println(user.age)
}

Error Handling

// Demonstrating error handling
fn divide(a int, b int) ?int {
    if b == 0 {
        return error("Division by zero")
    }
    return a / b
}

fn main() {
    result := divide(10, 2) or {
        println(err) // Handle the error
        return
    }
    println("Result: $result")
}

Modules

// Using modules in V
module mymodule

pub fn greet() string {
    return "Hello from mymodule!"
}

fn main() {
    import mymodule
    println(mymodule.greet())
}

Generics

// Generic functions in V
fn swap<T>(a T, b T) (T, T) {
    return b, a
}

fn main() {
    x, y := swap(5, 10)
    println("x: $x, y: $y")
}

Concurrency

// Simple concurrency example using tasks
fn print_numbers() {
    for i := 1; i <= 5; i++ {
        println(i)
    }
}

fn main() {
    go print_numbers()
    println("Task started")
}