func(ctx *ReceiveContext) that processes messages. The default behavior is the actor’s Receive method.
Behavior type
Behavior type
API
- ctx.Become: 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: 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
An account actor that switches behaviors based on authentication and transfer confirmation: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.