Command Definitions
Literals
Section titled “Literals”| Comment |
/* */ or //
|
| Boolean |
true, false
|
| Integer |
42, 0x2A, 0o52, 0b101010
|
| Decimal |
0.5, .5
|
| String |
"foo", 'bar'
|
| Array |
[1, 2, 3]
|
| Map |
{a: 1, b: 2, c: 3}
|
| Null |
nil
|
Strings
Section titled “Strings”Strings can be enclosed in single or double quotes. They can contain escape sequences, such as \n for newline,
\t for tab, \uXXXX for Unicode code points.
"Hello\nWorld"For multiline strings, use backticks:
`HelloWorld`Backtick strings are raw strings, they don’t support escape sequences.
Operators
Section titled “Operators”| Arithmetic |
+, -, *, /, % (modulo), ^ or ** (exponent)
|
| Comparison |
==, !=, <, >, <=, >=
|
| Logical |
not or !, and or &&, or or ||
|
| Conditional |
?: (ternary), ?? (null coalescing), if {} else {} (multiline)
|
| Membership |
[], ., ?., in
|
| String |
+ (concatenation), contains, startsWith, endsWith
|
| Regular Expression |
matches
|
| Range |
..
|
| Slice |
[:]
|
| Pipeline |
|
|
Membership Operator
Section titled “Membership Operator”Structure fields and map elements can be accessed with the . operator
or the [] operator. The following two expressions are equivalent:
user.Nameuser["Name"]Array and slice elements can be accessed with the [] operator.
Negative indices are supported, with -1 being the last element.
array[0] // first elementarray[-1] // last elementThe in operator can be used to check if an element is in an array or a map.
"John" in ["John", "Jane"]"name" in {"name": "John", "age": 30}Optional Chaining
Section titled “Optional Chaining”The ?. operator can be used to access a structure field or map element
without checking if the structure or map is nil. If the structure or map is
nil, the result of the expression is nil.
author.User?.NameIs equivalent to:
author.User != nil ? author.User.Name : nilNull Coalescing
Section titled “Null Coalescing”The ?? operator can be used to return the left side if it’s not nil,
otherwise return the right side.
author.User?.Name ?? "Anonymous"Is equivalent to:
author.User != nil ? author.User.Name : "Anonymous"Slice Operator
Section titled “Slice Operator”The slice operator can be used to extract a portion of an array or string.
The syntax is [start:end], where start and end are optional.
If start is omitted, it defaults to 0.
If end is omitted, it defaults to the length of the array or string.
Negative indices are supported.
array[1:3] // elements at index 1 and 2array[:3] // first three elementsarray[1:] // all elements starting from index 1array[-2:] // last two elementsString Formatting
Section titled “String Formatting”String formatting is done with the format function:
format("Hello %s", "World")format("Number: %d", 42)format("Float: %.2f", 3.14159)The following format specifiers are supported:
| %s | String |
| %d | Integer |
| %f | Float |
| %t | Boolean |
| %v | Any value |
For floating point numbers, you can specify the precision with .N where N is the number of decimal places:
format("%.2f", 3.14159) // "3.14"You can also specify the width of the field with a number before the format specifier:
format("%5d", 42) // " 42"format("%-5d", 42) // "42 " (left-aligned)format("%05d", 42) // "00042" (zero-padded)