Skip to content

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.

model Team:
name: String
members: Array[String] # Array of strings
scores: Array[Int] # Array of integers
model Matrix:
data: Array[Array[Float]] # 2D array of floats
model User:
name: String
tags: Array[String]
model Project:
title: String
users: Array[User] # Array of model instances

Arrays are validated at compile time:

team = Team(name="Alpha", members=["Alice", "Bob"])
# Valid — all elements are String
team.members = ["Alice", "Bob", "Charlie"]
# Invalid — the transpiler rejects this (Int is not String)
# team.members = ["Alice", 42]

Go:

type Team struct {
Name string
Members []string
Scores []int
}

Python:

class Team(BaseModel):
name: str
members: list[str]
scores: list[int]

Arrays are stored in the database based on the backend:

  • PostgreSQL: Native ARRAY type
  • 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]
);

PyGo provides a set of operations that work on arrays in both Go and Python:

team.members.append("David")
team.members.extend(["Eve", "Frank"])
handler listActiveUsers:
list(team_id: UUID) -> Array[User]:
team = Team.find(team_id)
return team.members.filter(active=True)
count = len(team.members)

Use Array[T]? for nullable arrays:

model Team:
name: String
members: Array[String]? = None # Can be NULL
  • 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)