Modules and Project Organization

15.1 Creating Modules

A module = a directory. All .v files in the directory belong to the same module (each declares module name).

Platform-specific files: file_linux.v, file_windows.v, file_macos.v, file_nix.v, file_default.c.v, file.js.v (JS backend), file_d_customflag.v (compiled with -d customflag), file_notd_customflag.v.

Files ending in _test.v are tests.

Circular imports are not allowed.

myproject/
├── v.mod
├── main.v
└── mymath/
    ├── mymath.v
    ├── geometry.v
    └── mymath_test.v
// mymath/mymath.v
module mymath

pub const golden = 1.618

pub fn add(a int, b int) int {
	return a + b
}

fn helper() {}          // private to the module

fn init() {             // module initializer: runs once at program start
	println('mymath initialized')
}
// main.v
module main
import mymath
import mymath { add }           // selective import
import mymath as mm             // alias

fn main() {
	println(mymath.add(1, 2))
	println(add(3, 4))
	println(mm.golden)
}

15.2 v.mod

Module {
	name: 'myproject'
	description: 'A sample V project'
	version: '0.1.0'
	license: 'MIT'
	dependencies: ['vsl', 'markdown']
}

15.3 Module Lookup Paths

The current project, ~/.vmodules (installed packages), and V's vlib (stdlib).

Customize with VMODULES or -path.

15.4 Scaffolding

v new myapp          # interactive: bin / lib / web templates
v init               # in an existing directory