2026-03-21 10:05:04 +00:00
|
|
|
---
|
|
|
|
|
title: Core Primitives
|
|
|
|
|
description: The repeated shapes that make CoreGO easy to navigate.
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# Core Primitives
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
CoreGO is built from a small vocabulary repeated everywhere.
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
## Primitive Map
|
|
|
|
|
|
|
|
|
|
| Type | Used For |
|
|
|
|
|
|------|----------|
|
2026-03-25 17:13:27 +00:00
|
|
|
| `Option` / `Options` | Input values and metadata |
|
2026-03-21 10:05:04 +00:00
|
|
|
| `Result` | Output values and success state |
|
|
|
|
|
| `Service` | Lifecycle-managed components |
|
2026-03-25 17:13:27 +00:00
|
|
|
| `Action` | Named callable with panic recovery + entitlement |
|
|
|
|
|
| `Task` | Composed sequence of Actions |
|
|
|
|
|
| `Registry[T]` | Thread-safe named collection |
|
|
|
|
|
| `Entitlement` | Permission check result |
|
|
|
|
|
| `Message` | Broadcast events (ACTION) |
|
|
|
|
|
| `Query` | Request-response lookups (QUERY) |
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
## `Option` and `Options`
|
|
|
|
|
|
|
|
|
|
`Option` is one key-value pair. `Options` is an ordered slice of them.
|
|
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
opts := core.NewOptions(
|
|
|
|
|
core.Option{Key: "name", Value: "brain"},
|
|
|
|
|
core.Option{Key: "path", Value: "prompts"},
|
|
|
|
|
core.Option{Key: "debug", Value: true},
|
|
|
|
|
)
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
name := opts.String("name")
|
|
|
|
|
debug := opts.Bool("debug")
|
2026-03-25 17:13:27 +00:00
|
|
|
raw := opts.Get("name") // Result{Value, OK}
|
|
|
|
|
opts.Has("path") // true
|
|
|
|
|
opts.Len() // 3
|
2026-03-21 10:05:04 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## `Result`
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
Universal return shape. Every Core operation returns Result.
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
type Result struct {
|
|
|
|
|
Value any
|
|
|
|
|
OK bool
|
|
|
|
|
}
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
r := c.Config().Get("host")
|
2026-03-21 10:05:04 +00:00
|
|
|
if r.OK {
|
2026-03-25 17:13:27 +00:00
|
|
|
host := r.Value.(string)
|
2026-03-21 10:05:04 +00:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
The `Result()` method adapts Go `(value, error)` pairs:
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
r := core.Result{}.Result(file, err)
|
2026-03-21 10:05:04 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## `Service`
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
Managed lifecycle component stored in the `ServiceRegistry`.
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
core.Service{
|
|
|
|
|
OnStart: func() core.Result { return core.Result{OK: true} },
|
|
|
|
|
OnStop: func() core.Result { return core.Result{OK: true} },
|
2026-03-21 10:05:04 +00:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
Or via `Startable`/`Stoppable` interfaces (preferred for named services):
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
```go
|
|
|
|
|
type Startable interface { OnStartup(ctx context.Context) Result }
|
|
|
|
|
type Stoppable interface { OnShutdown(ctx context.Context) Result }
|
|
|
|
|
```
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
## `Action`
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
Named callable — the atomic unit of work. Registered by name, invoked by name.
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
type ActionHandler func(context.Context, Options) Result
|
|
|
|
|
|
|
|
|
|
type Action struct {
|
|
|
|
|
Name string
|
|
|
|
|
Handler ActionHandler
|
|
|
|
|
Description string
|
|
|
|
|
Schema Options
|
|
|
|
|
}
|
2026-03-21 10:05:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
`Action.Run()` includes panic recovery and entitlement checking.
|
|
|
|
|
|
|
|
|
|
## `Task`
|
|
|
|
|
|
|
|
|
|
Composed sequence of Actions:
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
type Task struct {
|
|
|
|
|
Name string
|
|
|
|
|
Description string
|
|
|
|
|
Steps []Step
|
2026-03-21 10:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
type Step struct {
|
|
|
|
|
Action string
|
|
|
|
|
With Options
|
|
|
|
|
Async bool
|
|
|
|
|
Input string // "previous" = output of last step
|
2026-03-21 10:05:04 +00:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
## `Registry[T]`
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
Thread-safe named collection with insertion order and 3 lock modes:
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
r := core.NewRegistry[*MyService]()
|
|
|
|
|
r.Set("brain", svc)
|
|
|
|
|
r.Get("brain") // Result
|
|
|
|
|
r.Has("brain") // bool
|
|
|
|
|
r.Names() // []string (insertion order)
|
|
|
|
|
r.Each(func(name string, svc *MyService) { ... })
|
|
|
|
|
r.Lock() // fully frozen
|
|
|
|
|
r.Seal() // no new keys, updates OK
|
2026-03-21 10:05:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
## `Entitlement`
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
Permission check result:
|
2026-03-21 10:05:04 +00:00
|
|
|
|
|
|
|
|
```go
|
2026-03-25 17:13:27 +00:00
|
|
|
type Entitlement struct {
|
|
|
|
|
Allowed bool
|
|
|
|
|
Unlimited bool
|
|
|
|
|
Limit int
|
|
|
|
|
Used int
|
|
|
|
|
Remaining int
|
|
|
|
|
Reason string
|
2026-03-21 10:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
e := c.Entitled("social.accounts", 3)
|
|
|
|
|
e.NearLimit(0.8) // true if > 80% used
|
|
|
|
|
e.UsagePercent() // 75.0
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## `Message` and `Query`
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
IPC type aliases for the anonymous broadcast system:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type Message any // broadcast via ACTION
|
|
|
|
|
type Query any // request/response via QUERY
|
2026-03-21 10:05:04 +00:00
|
|
|
```
|
|
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
For typed, named dispatch use `c.Action("name").Run(ctx, opts)`.
|
|
|
|
|
|
|
|
|
|
## `ServiceRuntime[T]`
|
|
|
|
|
|
|
|
|
|
Composition helper for services that need Core access and typed options:
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
```go
|
|
|
|
|
type MyService struct {
|
|
|
|
|
*core.ServiceRuntime[MyOptions]
|
|
|
|
|
}
|
2026-03-21 10:05:04 +00:00
|
|
|
|
2026-03-25 17:13:27 +00:00
|
|
|
runtime := core.NewServiceRuntime(c, MyOptions{BufferSize: 1024})
|
|
|
|
|
runtime.Core() // *Core
|
|
|
|
|
runtime.Options() // MyOptions
|
|
|
|
|
runtime.Config() // shortcut to Core().Config()
|
|
|
|
|
```
|