Skip to content

Instantly share code, notes, and snippets.

@bgnori
Created February 25, 2014 18:20
Show Gist options
  • Save bgnori/9214691 to your computer and use it in GitHub Desktop.
Save bgnori/9214691 to your computer and use it in GitHub Desktop.
型アサーション(type assertion) と chan
package main
import (
"fmt"
"reflect"
)
type chanS interface{
Type() reflect.Type
}
func main() {
ch1 := make(chan int)
go func() {
for i := 0; i < 10; i++{
ch1<- i
}
close(ch1)
}()
for v := range ch1 {
fmt.Println(v)
}
s, ok := ch1.(chanS)
fmt.Println(ok)
}
prog.go:26: invalid type assertion: ch1.(chanS) (non-interface type chan int on left)
[process exited with non-zero status]
@bgnori
Copy link
Author

bgnori commented Feb 26, 2014

s, ok := interface{}(ch1).(chanS)
fmt.Println(ok, s)

via http://mattn.kaoriya.net/software/lang/go/20130919023425.htm

@bgnori
Copy link
Author

bgnori commented Feb 26, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment