Sequential Constructs
Variables and Expressions
Data Types
The supported (primitive) data types are shown in the following table:
| Data Type | Description |
|---|---|
int32 | 32-bit signed integer |
float32 | 32-bit floating point number |
bool | Boolean value |
string | String value |
The type int is not supported. Use int32 instead.
Variable Declaration and Assignment
Variables are declared using the var keyword or the := operator. The syntax is as follows:
var w = 5
var x int32
var y float32 = 5
z := 5
Assignment is done using the = operator. The syntax is as follows:
x = 5
Operators
The following operators are supported, in order of decreasing precedence:
| Operator | Description | Associativity |
|---|---|---|
+ - ! | Unary | Right to left |
* / % | Multiplicative | Left to right |
+ - | Additive | Left to right |
< <= > >= | Relational | Left to right |
== != | Equality | Left to right |
&& | Logical AND | Left to right |
|| | Logical OR | Left to right |
Additionally, we support postfix increment and decrement operators, given that they are used as a statement and not as an expression. For example, i++ is valid, but j = i++ is not valid.
Control Flow
If-Else
The syntax for the if-else construct is as follows:
if condition {
// code
} else {
// code
}
For Loop
The syntax for the for loop is as follows:
for i := 0; i < 5; i++ {
// code
}
The init and post statements can be empty, in such case the for loop can be used as a while loop.
for ; i < 5; {
// code
}
Break and continue statements are supported.
for i := 0; i < 5; i++ {
if i == 3 {
continue
}
if i == 4 {
break
}
fmt.Println(i)
}
Functions
The syntax for a function definition is as follows:
func add(a int32, b int32) int32 {
return a + b
}
Alternatively, one can use the func keyword to define a lambda expression.
add := func(a int32, b int32) int32 {
return a + b
}
Function calls can either be sequential or concurrent, using the go keyword.
add(1, 2) // Sequential
go add(1, 2) // Concurrent
Built-in Functions
The following built-in functions are supported:
- Default:
len,cap,append fmt:Println,Print