| 1 | <h2>Comments</h2> |
| 2 | <pre><code>// This is a comment |
| 3 | /* This is a comment |
| 4 | on multiple lines */</code></pre> |
| 5 | |
| 6 | <h2>Numbers</h2> |
| 7 | <pre><code>123 |
| 8 | 0x7B |
| 9 | 0b01111011 |
| 10 | 0o173 |
| 11 | 170141183460469231731687303715884105727 |
| 12 | 1_000_000 |
| 13 | 0b0_11 |
| 14 | 3_122.55 |
| 15 | 0xF_F |
| 16 | 0o17_3 |
| 17 | 72.40 |
| 18 | 072.40 |
| 19 | 2.71828 |
| 20 | </code></pre> |
| 21 | |
| 22 | <h2>Runes and strings</h2> |
| 23 | <pre><code>'\t' |
| 24 | '\000' |
| 25 | '\x07' |
| 26 | '\u12e4' |
| 27 | '\U00101234' |
| 28 | `abc` |
| 29 | `multi-line |
| 30 | string` |
| 31 | "Hello, world!" |
| 32 | "multi-line |
| 33 | string"</code></pre> |
| 34 | |
| 35 | <h2>String interpolation</h2> |
| 36 | <pre><code>'Hello, $name!' |
| 37 | "age = $user.age" |
| 38 | 'can register = ${user.age > 13}' |
| 39 | 'x = ${x:4.2f}' |
| 40 | '[${x:10}]' |
| 41 | '[${int(x):-10}]' |
| 42 | </code></pre> |
| 43 | |
| 44 | <h2>Struct</h2> |
| 45 | <pre><code>struct Foo { |
| 46 | a int // private immutable (default) |
| 47 | mut: |
| 48 | b int // private mutable |
| 49 | c int // (you can list multiple fields with the same access modifier) |
| 50 | pub: |
| 51 | d int // public immutable (readonly) |
| 52 | pub mut: |
| 53 | e int // public, but mutable only in parent module |
| 54 | __global: |
| 55 | f int // public and mutable both inside and outside parent module |
| 56 | } // (not recommended to use, that's why the 'global' keyword |
| 57 | // starts with __) |
| 58 | </code></pre> |
| 59 | |
| 60 | <h2>Functions</h2> |
| 61 | <pre><code>func(a, b int, z float64) bool { return a*b < int(z) }</code></pre> |
| 62 | |
| 63 | <h2>Full example</h2> |
| 64 | <pre><code> |
| 65 | module mymodule |
| 66 | |
| 67 | import external_module |
| 68 | |
| 69 | fn sqr(n int) int { |
| 70 | return n * n |
| 71 | } |
| 72 | |
| 73 | fn run(value int, op fn (int) int) int { |
| 74 | return op(value) |
| 75 | } |
| 76 | |
| 77 | fn main() { |
| 78 | println(run(5, sqr)) // "25" |
| 79 | // Anonymous functions can be declared inside other functions: |
| 80 | double_fn := fn (n int) int { |
| 81 | return n + n |
| 82 | } |
| 83 | println(run(5, double_fn)) // "10" |
| 84 | // Functions can be passed around without assigning them to variables: |
| 85 | res := run(5, fn (n int) int { |
| 86 | return n + n |
| 87 | }) |
| 88 | |
| 89 | external_module.say_hi() |
| 90 | } |
| 91 | </code></pre> |