Skip to main content

Tengo Language Overview

Tengo is a small, fast, and expressive scripting language designed for simplicity and embeddability. It is particularly well-suited for tasks that require scripting capabilities within a larger application, such as customizing workflows or automating tasks within the Platforma SDK. Below is an overview of the core concepts and features of Tengo to help you get started.

Syntax and Structure

Tengo's syntax is designed to be simple and familiar to users with experience in languages like Go or JavaScript. Here are the basic elements:

Variables and Constants

  • Variables are declared with := and can hold values that may change:

    a := 5
    b := "Hello, Tengo!"
  • Constants are declared with const and cannot be changed after they are set:

    const pi = 3.14

Data Types

Tengo supports several basic data types:

  • Integers and floats for numeric values:

    num := 42
    decimal := 3.14
  • Strings for text:

    text := "Hello, world!"
  • Booleans for true/false values:

    isTrue := true
  • Arrays for ordered collections of elements:

    arr := [1, 2, 3, "four"]
  • Maps for key-value pairs:

    person := {"name": "Alice", "age": 30}

Control Structures

Tengo provides standard control structures for managing the flow of execution:

  • If statements for conditional logic:

    if a > 10 {
    println("a is greater than 10")
    } else {
    println("a is 10 or less")
    }
  • For loops for iteration:

    for i := 0; i < 5; i++ {
    println(i)
    }

Functions

Functions in Tengo are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions:

add := func(x, y) {
return x + y
}

result := add(3, 4) // result is 7

Error Handling

Tengo uses a special error type for error handling. Functions can return an error, which can be checked and handled:

div := func(x, y) {
if y == 0 {
return error("division by zero")
}
return x / y
}

result, err := div(10, 0)
if err != undefined {
println(err) // Output: division by zero
}

Embedding and Integration

One of Tengo's strengths is its ability to be embedded within Go applications, making it a powerful tool for scripting within larger systems. The language is designed to be lightweight and fast, making it suitable for performance-sensitive applications.

Conclusion

Tengo is an excellent choice for scripting within the Platforma SDK, offering a balance of simplicity, performance, and expressiveness. With its straightforward syntax and rich feature set, Tengo allows developers to efficiently implement complex logic and automate workflows within their applications.

For more detailed information, you can refer to the Tengo documentation and explore examples and advanced usage.