github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/requestresponse/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	console "github.com/AsynkronIT/goconsole"
     8  	"github.com/AsynkronIT/protoactor-go/actor"
     9  )
    10  
    11  type Hello struct{ Who string }
    12  
    13  func Receive(context actor.Context) {
    14  	switch msg := context.Message().(type) {
    15  	case Hello:
    16  		context.Respond("Hello " + msg.Who)
    17  	}
    18  }
    19  
    20  func main() {
    21  	rootContext := actor.EmptyRootContext
    22  	props := actor.PropsFromFunc(Receive)
    23  	pid := rootContext.Spawn(props)
    24  	result, _ := rootContext.RequestFuture(pid, Hello{Who: "Roger"}, 30*time.Second).Result() // await result
    25  
    26  	fmt.Println(result)
    27  	console.ReadLine()
    28  }