Skip to content

Instantly share code, notes, and snippets.

@bgnori
Last active August 29, 2015 13:56
Show Gist options
  • Save bgnori/9213637 to your computer and use it in GitHub Desktop.
Save bgnori/9213637 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type T interface{}
type S interface{}
type U interface{}
func chzip(ch1 chan T, ch2 chan S, foo func(t T, s S) U) chan U {
var u = make(chan U)
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 <- foo(v1, v2)
}
}()
return u
}
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
adder := func(a, b int) int { return a + b }
ch3 := chzip(ch1, ch2, adder)
fmt.Println("hello interface")
}
prog.go:54: cannot use ch1 (type chan int) as type chan T in function argument
prog.go:54: cannot use ch2 (type chan int) as type chan S in function argument
prog.go:54: cannot use adder (type func(int, int) int) as type func(T, S) U in function argument
[process exited with non-zero status]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment