Skip to content

Instantly share code, notes, and snippets.

@rubensayshi
Created April 24, 2017 08:02
Show Gist options
  • Save rubensayshi/14380b5ccb6a153713552183820b834d to your computer and use it in GitHub Desktop.
Save rubensayshi/14380b5ccb6a153713552183820b834d to your computer and use it in GitHub Desktop.
package main
import (
"log"
"sync"
)
type Type int
const (
TypeAdd Type = 1
TypeDel Type = 2
)
type CM interface {
Channel() Type
}
type Add struct {
}
func (cm *Add) Channel() Type {
return TypeAdd
}
var _ CM = &Add{}
type Del struct {
}
func (cm *Del) Channel() Type {
return TypeDel
}
var _ CM = &Del{}
func work(incoming CM) {
if add, ok := (incoming).(*Add); ok {
log.Printf("Channel: %d\n", add.Channel())
} else if del, ok := (incoming).(*Del); ok {
log.Printf("Channel: %d\n", del.Channel())
} else {
log.Println("No known type to assert")
}
}
func main() {
log.Println("execute!")
c := make(chan CM, 2)
c <- &Del{}
c <- &Add{}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
work(<-c)
work(<-c)
wg.Done()
}()
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment