Flow
- Create an actor system with
NewActorSystemandStart - Spawn an actor with
Spawn(ctx, name, actor)β receive a*PID - Send messages with
NoSender().Tell(fire-and-forget) orNoSender().Ask(request-response) - Shutdown with
Stopwhen the application exits
Actor interface
Implement theActor interface to define your actor. See Actor Model
for the full interface and method explanations.
Messaging from outside
From outside the actor system (e.g.,main), use NoSender() to send:
- Tell β Fire-and-forget. No response expected.
- Ask β Request-response. Blocks until reply or timeout. The actor must call
ctx.Response(resp)to complete the request.
Replying from an actor
When handling a message sent viaAsk, call ctx.Response(resp) exactly once. The caller receives that value. For
Tell, no reply is expected.
ReceiveContext
InsideReceive, ctx provides:
| Method | Purpose |
|---|---|
Message() | The message being processed |
Sender() | PID of the sender (nil for scheduled or system messages) |
Self() | PID of the current actor |
Tell(to, msg) | Send fire-and-forget to another actor |
Ask(to, msg, timeout) | Send request-response |
Response(resp) | Reply to an Ask |
Unhandled() | Mark message as unhandled |