Fire-and-Forget Invocations in Go (Trigger)
Overview
Method.Trigger(client, in) enqueues an invocation without waiting for its result and returns an InvocationID, or an error if the invocation could not be enqueued (MustTrigger panics instead). Use it for notifications, fan-out, or handing work to a background/worker agent. Unlike a synchronous Call, Trigger does not block — so it is also safe for an agent to message itself (a synchronous self-Call would deadlock, since one instance handles one invocation at a time).
Steps
- Import the target agent's definition package.
- Get a client with
Agent.Get(id).
- Trigger the method:
Target.Method.MustTrigger(client, in) — returns immediately.
Example
package impl
import (
"myapp/agents/coordinator"
"myapp/agents/worker"
"github.com/golemcloud/golem/sdks/go/golem"
)
type state struct{}
var agent = coordinator.Agent.Implement(func(coordinator.ID) *state { return &state{} })
func init() {
agent.Handle(coordinator.Start, func(_ *golem.Context[state], in coordinator.StartIn) golem.Unit {
// Hand each unit of work to a worker without awaiting the outcome.
for _, w := range in.Jobs {
c := worker.Agent.Get(worker.ID{Name: w.Worker})
worker.Dispatch.MustTrigger(c, worker.DispatchIn{Job: w.Job})
}
return golem.Unit{}
})
}
Fanning out is just a loop of Trigger calls — each runs independently on its target agent.
Trigger vs Call
Call(c, in) (Out, error) / MustCall(c, in) Out — blocks, returns the result. Use when you need the value. Never self-Call.
Trigger(c, in) (InvocationID, error) / MustTrigger(c, in) InvocationID — returns immediately, no result. Use for notifications, fan-out, and self-messaging.
CallAsync(c, in) *Future[Out] — start now, await later with Get() or MustGet(). Use when you want the result but not to block yet (see golem-call-another-agent-go).
The returned InvocationID is an opaque handle to the enqueued invocation; you can ignore it for pure fire-and-forget.
Key Constraints
Trigger delivers a value-less enqueue: there is no way to read a return value from it. If you need the result, use Call/CallAsync.
- As with all RPC,
Trigger is called on the method descriptor (worker.Dispatch.Trigger(client, in)), and the client comes from the callee's definition (worker.Agent.Get(id)).
Related Skills
| Skill |
When to Load |
golem-call-another-agent-go |
You need the callee's result (sync Call / async CallAsync) |
golem-recurring-task-go |
Schedule a future/self invocation at a specific time |
golem-add-agent-go |
Define the agent being triggered |
1---2name: golem-fire-and-forget-go3description: "Fire-and-forget agent invocations in Go with Trigger. Use when the user wants to enqueue work without awaiting a result, send a notification to another agent, fan out to many agents, or kick off background work in a Go Golem project."4metadata:5 source: https://skillmd.com/skills/golemcloud/golem-fire-and-forget-go6---78# Fire-and-Forget Invocations in Go (`Trigger`)910## Overview1112`Method.Trigger(client, in)` enqueues an invocation **without waiting for its result** and returns an `InvocationID`, or an error if the invocation could not be enqueued (`MustTrigger` panics instead). Use it for notifications, fan-out, or handing work to a background/worker agent. Unlike a synchronous `Call`, `Trigger` does not block — so it is also safe for an agent to message **itself** (a synchronous self-`Call` would deadlock, since one instance handles one invocation at a time).1314## Steps15161. **Import the target agent's definition package.**172. **Get a client** with `Agent.Get(id)`.183. **Trigger the method**: `Target.Method.MustTrigger(client, in)` — returns immediately.1920## Example2122```go23package impl2425import (26 "myapp/agents/coordinator"27 "myapp/agents/worker"2829 "github.com/golemcloud/golem/sdks/go/golem"30)3132type state struct{}3334var agent = coordinator.Agent.Implement(func(coordinator.ID) *state { return &state{} })3536func init() {37 agent.Handle(coordinator.Start, func(_ *golem.Context[state], in coordinator.StartIn) golem.Unit {38 // Hand each unit of work to a worker without awaiting the outcome.39 for _, w := range in.Jobs {40 c := worker.Agent.Get(worker.ID{Name: w.Worker})41 worker.Dispatch.MustTrigger(c, worker.DispatchIn{Job: w.Job})42 }43 return golem.Unit{}44 })45}46```4748Fanning out is just a loop of `Trigger` calls — each runs independently on its target agent.4950## Trigger vs Call5152- `Call(c, in) (Out, error)` / `MustCall(c, in) Out` — blocks, returns the result. Use when you need the value. Never self-`Call`.53- `Trigger(c, in) (InvocationID, error)` / `MustTrigger(c, in) InvocationID` — returns immediately, no result. Use for notifications, fan-out, and self-messaging.54- `CallAsync(c, in) *Future[Out]` — start now, await later with `Get()` or `MustGet()`. Use when you want the result but not to block yet (see `golem-call-another-agent-go`).5556The returned `InvocationID` is an opaque handle to the enqueued invocation; you can ignore it for pure fire-and-forget.5758## Key Constraints5960- `Trigger` delivers a value-less enqueue: there is no way to read a return value from it. If you need the result, use `Call`/`CallAsync`.61- As with all RPC, `Trigger` is called on the **method descriptor** (`worker.Dispatch.Trigger(client, in)`), and the client comes from the callee's **definition** (`worker.Agent.Get(id)`).6263### Related Skills6465| Skill | When to Load |66|-------|--------------|67| `golem-call-another-agent-go` | You need the callee's result (sync `Call` / async `CallAsync`) |68| `golem-recurring-task-go` | Schedule a future/self invocation at a specific time |69| `golem-add-agent-go` | Define the agent being triggered |