Comments

// This is a comment
/* This is a comment
on multiple lines */

Numbers

123
0x7B
0b01111011
0o173
170141183460469231731687303715884105727
1_000_000
0b0_11
3_122.55
0xF_F
0o17_3
72.40
072.40
2.71828

Runes and strings

'\t'
'\000'
'\x07'
'\u12e4'
'\U00101234'
`abc`
`multi-line
string`
"Hello, world!"
"multi-line
string"

String interpolation

'Hello, $name!'
"age = $user.age"
'can register = ${user.age > 13}'
'x = ${x:4.2f}'
'[${x:10}]'
'[${int(x):-10}]'

Struct

struct Foo {
	a int   // private immutable (default)
mut:
	b int   // private mutable
	c int   // (you can list multiple fields with the same access modifier)
pub:
	d int   // public immutable (readonly)
pub mut:
	e int   // public, but mutable only in parent module
__global:
	f int   // public and mutable both inside and outside parent module
}           // (not recommended to use, that's why the 'global' keyword
			// starts with __)

Functions

func(a, b int, z float64) bool { return a*b < int(z) }

Full example


module mymodule

import external_module

fn sqr(n int) int {
	return n * n
}

fn run(value int, op fn (int) int) int {
	return op(value)
}

fn main() {
	println(run(5, sqr)) // "25"
	// Anonymous functions can be declared inside other functions:
	double_fn := fn (n int) int {
		return n + n
	}
	println(run(5, double_fn)) // "10"
	// Functions can be passed around without assigning them to variables:
	res := run(5, fn (n int) int {
		return n + n
	})

	external_module.say_hi()
}