Array[T]
Array[T]
Section titled “Array[T]”PyGo provides first-class support for generic array types using the Array[T] syntax. Arrays are type-safe at compile time and mechanically transpiled to idiomatic Go slices and Python lists.
Basic Usage
Section titled “Basic Usage”model Team: name: String members: Array[String] # Array of strings scores: Array[Int] # Array of integersNested Arrays
Section titled “Nested Arrays”model Matrix: data: Array[Array[Float]] # 2D array of floatsArrays of Models
Section titled “Arrays of Models”model User: name: String tags: Array[String]
model Project: title: String users: Array[User] # Array of model instancesType Safety
Section titled “Type Safety”Arrays are validated at compile time:
team = Team(name="Alpha", members=["Alice", "Bob"])
# Valid — all elements are Stringteam.members = ["Alice", "Bob", "Charlie"]
# Invalid — the transpiler rejects this (Int is not String)# team.members = ["Alice", 42]Generated Code
Section titled “Generated Code”Go:
type Team struct { Name string Members []string Scores []int}Python:
class Team(BaseModel): name: str members: list[str] scores: list[int]Database Representation
Section titled “Database Representation”Arrays are stored in the database based on the backend:
- PostgreSQL: Native
ARRAYtype - SQLite: JSON-encoded array
-- SQLite (JSON-encoded)CREATE TABLE team ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, members TEXT, -- JSON: ["Alice", "Bob", "Charlie"] scores TEXT -- JSON: [10, 20, 30]);Array Operations
Section titled “Array Operations”PyGo provides a set of operations that work on arrays in both Go and Python:
Append
Section titled “Append”team.members.append("David")Extend
Section titled “Extend”team.members.extend(["Eve", "Frank"])Filter (in handler)
Section titled “Filter (in handler)”handler listActiveUsers: list(team_id: UUID) -> Array[User]: team = Team.find(team_id) return team.members.filter(active=True)Length
Section titled “Length”count = len(team.members)Nullable Arrays
Section titled “Nullable Arrays”Use Array[T]? for nullable arrays:
model Team: name: String members: Array[String]? = None # Can be NULLBest Practices
Section titled “Best Practices”- Use
Array[String]for homogeneous string collections - Use
Array[Model]for embedded model references - Avoid arrays of arrays for better database compatibility
- Use
Array[String]?for optional arrays (can be NULL)