I have a problem I don't understand:
Why does the following TypeCounts element not implement Recorder?
See: http://play.golang.org/p/qHa0IReHr-
package main
import (
"fmt"
"reflect"
"strconv"
)
type Type int
func (t Type) String() string { return "type" }
type TypeCount struct {
Type Type
Count int
}
func (t TypeCount) Header() []string { return []string{"type", "count"} }
func (t TypeCount) Record() []string {
return []string{t.Type.String(), strconv.FormatInt(int64(t.Count), 10)}
}
var _ Recorder = (*TypeCount)(nil) // this is fine
type TypeCounts []TypeCount
func (ts TypeCounts) Len() int { return len(ts) }
func (ts TypeCounts) Less(i, j int) bool { return ts[i].Type.String() <
ts[j].Type.String() }
func (ts TypeCounts) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
type Recorder interface {
Header() []string
Record() []string
}
func RecorderSlice(v interface{}) {
// Check if v is of the right type.
t := reflect.TypeOf(v)
rt := reflect.TypeOf((*Recorder)(nil)).Elem()
if t.Kind() != reflect.Slice && t.Kind() != reflect.Array {
fmt.Printf("type %v is not slice or array\n", t)
} else if t.Elem().Implements(rt) {
fmt.Printf("element type %v does not implement %v\n", t.Elem(), rt)
}
}
func main() {
ts := make(TypeCounts, 0)
// Prints: element type main.TypeCount does not implement main.Recorder
RecorderSlice(ts)
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.