Branch
Hash :
72962701
Author :
Date :
2021-01-08T22:22:50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<h2>Comments</h2>
<pre><code>// This is a comment
/* This is a comment
on multiple lines */</code></pre>
<h2>Numbers</h2>
<pre><code>123
0x7B
0b01111011
0o173
170141183460469231731687303715884105727
1_000_000
0b0_11
3_122.55
0xF_F
0o17_3
72.40
072.40
2.71828
</code></pre>
<h2>Runes and strings</h2>
<pre><code>'\t'
'\000'
'\x07'
'\u12e4'
'\U00101234'
`abc`
`multi-line
string`
"Hello, world!"
"multi-line
string"</code></pre>
<h2>String interpolation</h2>
<pre><code>'Hello, $name!'
"age = $user.age"
'can register = ${user.age > 13}'
'x = ${x:4.2f}'
'[${x:10}]'
'[${int(x):-10}]'
</code></pre>
<h2>Struct</h2>
<pre><code>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 __)
</code></pre>
<h2>Functions</h2>
<pre><code>func(a, b int, z float64) bool { return a*b < int(z) }</code></pre>
<h2>Full example</h2>
<pre><code>
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()
}
</code></pre>