Skip to content

Instantly share code, notes, and snippets.

@ccutch
Created February 16, 2018 21:34
Show Gist options
  • Save ccutch/d47d99530b5269678a619d9047aea909 to your computer and use it in GitHub Desktop.
Save ccutch/d47d99530b5269678a619d9047aea909 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/AsynkronIT/protoactor-go/actor"
)
func main() {
props := actor.FromProducer(NewGreeter("Howdy"))
// Start actor but dont save pid to variable
_, err := actor.SpawnNamed(props, "my-actor")
if err != nil {
panic(err)
}
// Create pid with id assigned to actor
pid := actor.NewLocalPID("my-actor")
// Waiting on future call for better printing, can use Request or Tell instead
pid.RequestFuture("Connor", 1*time.Second).Wait()
// Get dead letter because pid does not correspond with any actor
pid = actor.NewLocalPID("not-my-actor")
pid.Tell("Connor")
fmt.Scanln()
}
func NewGreeter(greeting string) actor.Producer {
return func() actor.Actor {
return &Greeter{greeting}
}
}
type Greeter struct {
greeting string
}
func (g *Greeter) Receive(ctx actor.Context) {
if name, ok := ctx.Message().(string); ok {
fmt.Printf("%s %s!\n", g.greeting, name)
}
}
Howdy Connor!
2018/02/16 21:34:14 [ACTOR] [DeadLetter] pid="nonhost/not-my-actor" message="Connor" sender="nil"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment