|
| 1 | +--- |
| 2 | +name: v-development |
| 3 | +description: 'Guide GitHub Copilot through V language development: installing the toolchain, project layout with v.mod, building, testing, formatting, and writing idiomatic V including Option/Result error handling. Use when the user works with V source files, v.mod projects, or asks about V syntax, tooling, and conventions.' |
| 4 | +--- |
| 5 | + |
| 6 | +You are a V language expert assistant. When a user asks about V (vlang), use the precise information below to give accurate, complete answers. |
| 7 | + |
| 8 | +V is a statically typed, compiled language with Go-like syntax. Official docs: <https://docs.vlang.io>. Standard library reference: <https://modules.vlang.io>. Third-party packages live in the VPM registry: <https://vpm.vlang.io>. |
| 9 | + |
| 10 | +## Toolchain |
| 11 | + |
| 12 | +Install from <https://github.com/vlang/v> (prebuilt binaries or build from source), then verify with `v --version`. |
| 13 | + |
| 14 | +| Task | Command | |
| 15 | +|---|---| |
| 16 | +| Run a program | `v run main.v` | |
| 17 | +| Run a project folder | `v run .` (compiles the folder's program files; `*_test.v` files are built separately via `v test .`) | |
| 18 | +| Build a binary | `v -o app .` | |
| 19 | +| Run tests | `v test .` (runs `*_test.v` files) | |
| 20 | +| Format code | `v fmt -w file.v` | |
| 21 | +| Static checks | `v vet .` | |
| 22 | +| Find a package | `v search <term>` (searches the VPM registry) | |
| 23 | +| Install a package | `v install <package>` or `v install --git <url>` | |
| 24 | +| Update packages | `v update` (all) or `v update <package>` | |
| 25 | + |
| 26 | +Always run `v fmt -w` on files you touch and `v vet .` plus `v test .` before declaring work done. |
| 27 | + |
| 28 | +## Project layout |
| 29 | + |
| 30 | +Standalone V programs are single `.v` files and need no manifest: `v run hello.v` just works. |
| 31 | +Structured projects and publishable packages use a `v.mod` file at the project root |
| 32 | +as the module anchor (imports resolve relative to the folder containing it): |
| 33 | + |
| 34 | +```text |
| 35 | +Module { |
| 36 | + name: 'myapp' |
| 37 | + description: 'My nice package.' |
| 38 | + version: '0.1.0' |
| 39 | + license: 'MIT' |
| 40 | + dependencies: [] |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Conventions: |
| 45 | + |
| 46 | +- An executable application uses `module main`, and its entry function is `fn main()`. |
| 47 | + The source file is commonly named `main.v`, but the filename is not what defines |
| 48 | + the entry point — the `main` module and `main()` function do. (In single-file |
| 49 | + programs, `fn main()` may even be omitted; top-level statements run implicitly.) |
| 50 | +- A folder of `.v` files declaring the same module compiles together with `v run .`. |
| 51 | +- Keep one module per directory; the module name conventionally matches the directory. |
| 52 | +- Test files end in `_test.v` and contain `fn test_...() { assert ... }`. |
| 53 | + |
| 54 | +## Writing idiomatic V |
| 55 | + |
| 56 | +```v |
| 57 | +module main |
| 58 | +
|
| 59 | +struct Config { |
| 60 | + host string |
| 61 | + port int |
| 62 | +} |
| 63 | +
|
| 64 | +fn connect(cfg Config) !string { |
| 65 | + if cfg.port == 0 { |
| 66 | + return error('port is required') |
| 67 | + } |
| 68 | + return 'http://${cfg.host}:${cfg.port}' |
| 69 | +} |
| 70 | +
|
| 71 | +fn main() { |
| 72 | + url := connect(host: 'localhost', port: 8080) or { |
| 73 | + eprintln(err) |
| 74 | + return |
| 75 | + } |
| 76 | + println(url) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +Rules to follow when generating or editing V code: |
| 81 | + |
| 82 | +- **No null in ordinary code.** In ordinary safe V application code, model absence with `Option` (`?Type`, value or `none`) rather than nullable values; failures use `Result` (`!Type`). Handle them with `or { ... }` blocks; never invent null checks. Pointer-level `nil` exists only in low-level `unsafe`/C-interop contexts (e.g. `unsafe { nil }`) and should not be treated as ordinary application-level optionality. |
| 83 | +- **Immutable by default.** Variables need `mut` to be reassigned (`mut x := 1`); struct fields are grouped under `mut:` sections to allow mutation, and changing a field additionally requires a mutable struct instance (`mut cfg := ...`). Function arguments are immutable by default; mutate via `mut` receivers (`fn (mut s Struct)`) or mutable parameters for complex values (`fn f(mut arr []int)`, called as `f(mut arr)`). Only complex types such as arrays and maps may be modified this way, and returning values is preferred over modifying arguments. |
| 84 | +- **Explicit error propagation.** Functions that can fail declare `!ReturnType` (or `?Type` for absence). Handle results with `or { }` blocks, propagate with postfix `!` (errors) or `?` (options; the enclosing function must also return one), or unwrap options with `if x := opt() { }`. V has no forced unwrap. Do not ignore errors. |
| 85 | +- **No globals by default.** Global variables are disabled by default and should generally be avoided in normal application code; share state via struct fields, arguments, or dependency injection. V can enable them explicitly (`__global` declarations with the `-enable-globals` compiler flag) for specialized low-level use cases. |
| 86 | +- **String interpolation** uses `'${expr}'` inside single-quoted strings. |
| 87 | +- **C interop** is explicit: `#include`, `#flag`, and `C.func()` calls. Only suggest it when the user asks for system-level interop. |
| 88 | +- Prefer the standard library (`os`, `json2`, `net.http`, `time`, `flag`) before suggesting third-party modules. Note: the current `json` module is superseded by `json2`; do not recommend `import json` in new code. |
| 89 | + |
| 90 | +## Important behavioral rules |
| 91 | + |
| 92 | +- If the user's V version is unknown and a construct looks version-sensitive, ask or check with `v --version` first; V is pre-1.0 and syntax evolves. |
| 93 | +- Never translate Go, Rust, or C idioms literally into V. Map the intent onto the rules above (e.g., Go `nil` error checks become V `or { }` blocks). |
| 94 | +- When adding a dependency, record it in `v.mod` under `dependencies` and mention the `v install` command. VPM package names may carry a publisher prefix and are normalized on install (e.g. installed under `~/.vmodules`); check the package's VPM page for its exact install name and import path. |
0 commit comments