func(ctx *ReceiveContext) that processes messages. The default behavior is the actorβs Receive method.
Behavior type
API
| Method | Purpose |
|---|---|
ctx.Become(behavior) | Replace the current behavior. The current message finishes under the old behavior; subsequent messages use the new one. No stackβprevious behavior is lost. |
ctx.BecomeStacked(behavior) | Push a new behavior on top. Current message still uses the existing behavior; subsequent messages use the new one until UnBecomeStacked. |
ctx.UnBecomeStacked() | Pop the top behavior. Resume the previous one. No effect if the stack is empty. |
ctx.UnBecome() | Reset to the default (initial) behavior. Clears the stack. |
Become vs BecomeStacked
- Become β Swap behavior. Use when transitioning to a new state and you do not need to return to the previous one.
- BecomeStacked β Push behavior. Use when you need to temporarily handle messages differently, then pop back (e.g., a modal dialog, a temporary protocol phase).
Example
Stashing with behaviors
When switching behaviors, you may want to defer processing of the current message. Usectx.Stash() to buffer it, then ctx.Unstash() or ctx.UnstashAll() after changing behavior. Requires WithStashing() at spawn. See Stashing.