github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/mixins/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/AsynkronIT/goconsole" 7 "github.com/AsynkronIT/protoactor-go/actor" 8 "github.com/AsynkronIT/protoactor-go/actor/middleware" 9 ) 10 11 type myActor struct { 12 NameAwareHolder 13 } 14 15 func (state *myActor) Receive(context actor.Context) { 16 switch context.Message().(type) { 17 case *actor.Started: 18 // this actor have been initialized by the receive pipeline 19 fmt.Printf("My name is %v\n", state.name) 20 } 21 } 22 23 type NameAware interface { 24 SetName(name string) 25 } 26 27 type NameAwareHolder struct { 28 name string 29 } 30 31 func (state *NameAwareHolder) SetName(name string) { 32 state.name = name 33 } 34 35 type NamerPlugin struct{} 36 37 func (p *NamerPlugin) OnStart(ctx actor.ReceiverContext) { 38 if p, ok := ctx.Actor().(NameAware); ok { 39 p.SetName("GAM") 40 } 41 } 42 func (p *NamerPlugin) OnOtherMessage(ctx actor.ReceiverContext, env *actor.MessageEnvelope) {} 43 44 func main() { 45 rootContext := actor.EmptyRootContext 46 props := actor. 47 PropsFromProducer(func() actor.Actor { return &myActor{} }). 48 WithReceiverMiddleware( 49 middleware.Logger, 50 //plugin.Use(&NamerPlugin{}), 51 ) 52 53 pid := rootContext.Spawn(props) 54 rootContext.Send(pid, "bar") 55 console.ReadLine() 56 }