Skip to content

Instantly share code, notes, and snippets.

@lucasrpb
Created December 13, 2016 18:07
Show Gist options
  • Save lucasrpb/d015a95e25bc1d19573c05726aa14275 to your computer and use it in GitHub Desktop.
Save lucasrpb/d015a95e25bc1d19573c05726aa14275 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"bufio"
"os"
"math"
)
type ActorI interface {
Send(data interface{})
Receive(data interface{})
}
type Actor struct {
Name string
Mailbox chan interface{}
ActorI
}
func (actor Actor) Receive(data interface{}){
fmt.Println("Received:", data)
}
func (actor Actor) Send(data interface{}){
actor.Mailbox <- data
}
func (actor Actor) Handle(){
go func(){
for {
data, ok := <-actor.Mailbox
if ok {
actor.ActorI.Receive(data)
} else {
return
}
}
}()
}
type MyActor struct {
Actor
}
func (actor MyActor) Receive(data interface{}){
switch data.(type) {
case string: {
fmt.Println("STRING: ",data)
}
default: {
fmt.Println("\n\nI DONT KNOW...\n\n")
}
}
}
func NewActor(actori ActorI, name string) *Actor {
actor := Actor {
Name: name,
ActorI: actori,
Mailbox: make(chan interface{}, math.MaxInt8),
}
actor.Handle()
return &actor
}
func main() {
actor := NewActor(MyActor{}, "a")
actor.Handle()
actor.Send("Hi!")
reader := bufio.NewReader(os.Stdin)
reader.ReadLine()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment