github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/spawn_example_test.go (about)

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