Arrays
Arrays are ordered collections of values of the same type.
Array Declaration
Arrays are declared using square brackets [] followed by the element type.
Syntax
1
| var name []type = [elements]
|
Examples
1
2
3
4
| var empty []number = []
var numbers []number = [1, 2, 3, 4, 5]
var names []string = ["Alice", "Bob", "Charlie"]
var flags []bool = [true, false, true]
|
Array Types
Array type annotations use [] followed by the element type.
Common Array Types
[]number - Array of numbers[]string - Array of strings[]bool - Array of booleans
Accessing Elements
Array elements are accessed using the index operator [] with a zero-based index.
Reading Elements
1
2
3
4
5
| var fruits []string = ["apple", "banana", "cherry"]
printf("%s\n", fruits[0]) // "apple"
printf("%s\n", fruits[1]) // "banana"
printf("%s\n", fruits[2]) // "cherry"
|
Index Out of Bounds
Accessing an index that doesn’t exist results in an error:
1
2
3
| var numbers []number = [1, 2, 3]
printf("%g\n", numbers[10]) // Error: array index out of bounds
|
Modifying Elements
You can modify array elements by assigning to a specific index.
1
2
3
4
5
6
| var numbers []number = [1, 2, 3]
numbers[1] = 10
printf("%g\n", numbers[1]) // 10
printf("%s\n", numbers) // [1, 10, 3]
|
Array Operations
Concatenation
The + operator can be used to concatenate arrays:
1
2
3
4
5
| var arr1 []number = [1, 2, 3]
var arr2 []number = [4, 5, 6]
var combined []number = arr1 + arr2
printf("%s\n", combined) // [1, 2, 3, 4, 5, 6]
|
Reassignment
Arrays can be reassigned to new values:
1
2
3
4
5
6
| var array1 []number = []
printf("Empty array: %s\n", array1) // []
array1 = [4, 5, 6]
printf("Array with [4, 5, 6]: %s\n", array1) // [4, 5, 6]
|
Using Arrays with Functions
Passing Arrays as Arguments
Arrays can be passed to functions:
1
2
3
4
5
6
| func printArray(arr []number) {
printf("%s\n", arr)
}
var numbers []number = [1, 2, 3]
printArray(numbers)
|
Returning Arrays
Functions can return arrays:
1
2
3
4
5
| func getRange() []number {
return [1, 2, 3, 4, 5]
}
var numbers []number = getRange()
|
Spread Operator with Arrays
Use the spread operator (...) to expand array elements as individual function arguments:
1
2
3
4
5
6
7
8
| func sum(a number, b number, c number) number {
return a + b + c
}
var numbers []number = [1, 2, 3]
var total number = sum(...numbers) // expands to sum(1, 2, 3)
printf("%g\n", total) // 6
|
Iterating Over Arrays
Use a for loop to iterate over array indices:
1
2
3
4
5
| var fruits []string = ["apple", "banana", "cherry"]
for var i to 3 {
printf("%s\n", fruits[i])
}
|
Practical Examples
Building an Array
1
2
3
4
5
6
| var numbers []number = [1, 2, 3]
printf("Initial: %s\n", numbers)
numbers = numbers + [4, 5]
printf("After adding: %s\n", numbers) // [1, 2, 3, 4, 5]
|
Array Modification
1
2
3
4
5
6
7
8
| var array2 []number = [1, 2, 3, 4, 5, 6]
printf("array2[1]: %g\n", array2[1]) // 2
array2[1] = 10
printf("array2[1]: %g\n", array2[1]) // 10
printf("array2: %s\n", array2) // [1, 10, 3, 4, 5, 6]
|
Type Safety
All elements in an array must be of the same type.
Type mismatches will result in errors.
1
2
3
4
| var numbers []number = [1, 2, 3]
// This would cause an error:
// numbers = ["a", "b", "c"] // Error: type mismatch
|
Improve this page