Skip to content

Instantly share code, notes, and snippets.

@bgnori
Created February 26, 2014 01:02
Show Gist options
  • Save bgnori/9221353 to your computer and use it in GitHub Desktop.
Save bgnori/9221353 to your computer and use it in GitHub Desktop.
とりあえずbuildできるところまではきた. deadlockしてしまう.
package main
import (
//"reflect"
"fmt"
)
type T interface{}
type S interface{}
type U struct {
t T
s S
}
func chzip(ch1 chan T, ch2 chan S) chan U {
var u = make(chan U, 100)
go func() {
var v1 T
var v2 S
var ok1, ok2 bool
for {
select {
case v1, ok1 = <-ch1:
if !ok1 {
close(u)
return
}
v2, ok2 = <-ch2
if !ok2 {
close(u)
return
}
case v2, ok2 = <-ch2:
if !ok2 {
close(u)
return
}
v1, ok1 = <-ch1
if !ok1 {
close(u)
return
}
}
u <- U{v1, v2}
}
}()
return u
}
func main() {
ch1 := make(chan int, 100)
ch2 := make(chan int, 100)
var c1 chan T
var c2 chan S
c1, _ = interface{}(ch1).(chan T)
c2, _ = interface{}(ch2).(chan S)
ch3 := chzip(c1, c2)
go func() {
for i := 0; i < 10; i++{
ch1<- i
}
close(ch1)
}()
go func() {
for i := 100; i < 300; i+=10 {
ch2 <- i
}
close(ch2)
}()
fmt.Println("hello interface")
for v := range ch3 {
fmt.Println(v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment