github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/spawn_named_example_test.go (about) 1 package actor_test 2 3 import ( 4 "fmt" 5 "log" 6 "sync" 7 8 "github.com/asynkron/protoactor-go/actor" 9 ) 10 11 // Spawn creates instances of actors, similar to 'new' or 'make' but for actors. 12 func ExampleRootContext_SpawnNamed() { 13 var wg sync.WaitGroup 14 wg.Add(1) 15 16 // create root context 17 context := system.Root 18 19 // define the actor props. 20 // props define the creation process of an actor 21 props := actor.PropsFromFunc(func(ctx actor.Context) { 22 // check if the message is a *actor.Started message 23 // this is the first message all actors get 24 // actor.Started is received async and can be used 25 // to initialize your actors initial state 26 if _, ok := ctx.Message().(*actor.Started); ok { 27 fmt.Println("hello world") 28 wg.Done() 29 } 30 }) 31 32 // spawn the actor based on the props 33 _, err := context.SpawnNamed(props, "my-actor") 34 if err != nil { 35 log.Fatal("The actor name is already in use") 36 } 37 wg.Wait() 38 // Output: hello world 39 }