Queries in Go
Keep selection logic beside the state it returns. Arc.Go queries belong to an ordinary read-model struct, with unnamed value-receiver methods standing in for C# static methods. You can also use explicitly owned top-level functions.
Start with a snapshot, or register an observable source when you need live results. HTTP hosting supports both; handwritten registration remains the observable authoring path while generated observable adapters await their separate tools lane. Source directives do not register queries at runtime. No database or dependency-injection container is required.
Return a first snapshot
Section titled “Return a first snapshot”Save this complete program in a module that depends on Arc.Go, then run go run ..
It prints Ada.
package main
import ( "context" "fmt"
"github.com/cratis/arc.go/metadata" "github.com/cratis/arc.go/queries")
//arc:readmodeltype Author struct { Name string `json:"name"`}
func (Author) All(context.Context, queries.NoArguments) ([]Author, error) { return []Author{{Name: "Ada"}}, nil}
func run() error { var registry queries.Registry err := queries.Register[Author](®istry, "All", queries.Function(func(ctx context.Context, args queries.NoArguments) ([]Author, error) { return Author{}.All(ctx, args) }), queries.WithAuthorization[queries.NoArguments](metadata.Authorization{AllowAnonymous: true}), ) if err != nil { return err } pipeline, err := registry.Build(queries.PipelineOptions{}) if err != nil { return err } result, err := queries.Perform[[]Author](context.Background(), pipeline, "Author.All", queries.Request{}) if err != nil { return err } if !result.IsSuccess() { return fmt.Errorf("query failed with status %d", result.StatusCode()) } authors, _ := result.Data() fmt.Println(authors[0].Name) return nil}
func main() { if err := run(); err != nil { panic(err) }}Register records the owning model, query identity and typed callback. Build
validates the catalog without invoking the callback. Perform opens an operation
scope, checks security and validation, and returns a ready result. A successful nil
model is ready-null, not a pending query.
Choose the next step
Section titled “Choose the next step”- Model-bound authoring: namespace methods, directed functions and shared read models.
- Query arguments: explicit requiredness, defaults and presence.
- Paging: provider rendering versus already-windowed pages.
- Query pipeline: lifecycle, staging and local errors.
- Query results: the unchanged wire envelope and status precedence.
- Observable queries: owned sources, current values and subscriptions.
- Observable HTTP: current/pending/wait and direct transports.
- Observable hubs: revisions, ownership and transfer modes.