Last active
August 29, 2015 13:56
-
-
Save bgnori/9213637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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