V

From G1Joshi/Agent-Skills · View interactive version

V is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert V language (vlang) assistance covering fast compilation, memory safety, C interop, and concurrency. Use when writing fast lightweight binaries, web servers, or cross-platform utilities.

How to use this skill

Skill file

V (Vlang)

V is a simple, fast, and compiled language designed for building maintainable software with ultra-fast compilation speeds, memory safety, and minimal dependencies.

When to Use

Quick Start

struct User {
    id   int
    name string
}

fn main() {
    users := [User{id: 1, name: 'Alice'}, User{id: 2, name: 'Bob'}]
    for u in users {
        println('User $u.id: $u.name')
    }
}

Core Concepts

Memory Management & Immutability by Default

V uses compile-time autofree memory management without garbage collection pauses:

module main

import os

// Struct definition with explicit access controls
pub struct UserSession {
pub:
	username string
	user_id  int
pub mut:
	token    string
}

fn main() {
	// Variables are immutable by default
	user := UserSession{
		username: 'alice'
		user_id: 101
		token: 'tok-abc'
	}
	println('User: ${user.username}, ID: ${user.user_id}')
}

Result & Option Types for Error Propagation

Handling missing values and errors explicitly without exceptions:

module main

import os

struct Config {
	port int
	host string
}

fn parse_port(env_var string) !int {
	val := os.getenv(env_var)
	if val == '' {
		return error('Environment variable ${env_var} not found')
	}
	port := val.int()
	if port <= 0 || port > 65535 {
		return error('Port ${port} out of valid range (1-65535)')
	}
	return port
}

fn main() {
	port := parse_port('SERVER_PORT') or {
		eprintln('Failed to load port: ${err}')
		8080 // fallback default
	}
	println('Starting server on port ${port}')
}

Concurrency with Coroutines & Channels

Lightweight thread spawning with built-in CSP channels:

module main

import sync

fn worker(id int, ch chan int) {
	for i in 0 .. 3 {
		ch <- id * 10 + i
	}
}

fn main() {
	ch := chan int{cap: 10}

	// Spawn concurrent green threads with 'spawn' keyword
	spawn worker(1, ch)
	spawn worker(2, ch)

	for _ in 0 .. 6 {
		val := <-ch
		println('Received: ${val}')
	}
	ch.close()
}

Common Patterns

Result and Option Types for Robust Error Handling

Problem: Nil-pointer crashes and unhandled error states.

Solution:

Use V's built-in ? Option and ! Result return types:

fn divide(a f64, b f64) !f64 {
    if b == 0.0 {
        return error('division by zero')
    }
    return a / b
}

fn main() {
    res := divide(10.0, 2.0) or {
        eprintln('Calculation failed: $err')
        return
    }
    println('Result: $res')
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------ | :--------------------------------------------------------------- | :------------------------------------------------------------- |

| unhandled option / result | Function returning Option or Result called without or { ... }. | Handle error with or { ... } or propagate with ?. |

| variable '...' is immutable | Attempting to reassign an immutable variable. | Declare variable with mut: mut count := 0. |

| builder error: C compilation failed | Missing C compiler (gcc or clang) in system PATH. | Install gcc/clang and ensure it is available in terminal PATH. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.