Structs in AngelScript

Based on the provided steps and input, below is the translated code and explanation formatted in Markdown suitable for Hugo. The target language for translation is AngelScript.


Go’s structs are typed collections of fields. They’re useful for grouping data together to form records.

This person struct type has name and age fields.

class person {
    string name;
    int age;
}

newPerson constructs a new person struct with the given name.

person@ newPerson(const string &in name) {
    person p;
    p.name = name;
    p.age = 42;
    return p;
}

This syntax creates a new struct.

void main() {
    person p1 = {"Bob", 20};
    println(p1.name + " " + p1.age);
    
    person p2 = {name: "Alice", age: 30};
    println(p2.name + " " + p2.age);
    
    person p3 = {name: "Fred"};
    println(p3.name + " " + p3.age);

    person@ p4 = person();
    p4.name = "Ann";
    p4.age = 40;
    println(p4.name + " " + p4.age);
    
    person@ p5 = newPerson("Jon");
    println(p5.name + " " + p5.age);

    person s = {name: "Sean", age: 50};
    println(s.name);
    person@ sp = @s;
    println(sp.age);

    sp.age = 51;
    println(sp.age);
    
    auto dog = {"Rex", true};
    println(dog[0] + " " + (dog[1] ? "true" : "false"));
}

To run the program, use AngelScript’s scripting engine to execute it.


Next example: Methods.