FAQ
Hi,

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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Sebastien Binet at Dec 8, 2015 at 1:28 pm
    actually, it does as "t.Elem().Implements(rt)" is true and thus the
    message is displayed :)

    -s
    On Tue, Dec 8, 2015 at 1:43 PM, wrote:
    Hi,

    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kai Ding at Dec 8, 2015 at 1:40 pm
    see code:
    else if t.Elem().Implements(rt) {
             fmt.Printf("element type %v does not implement %v\n", t.Elem(), rt)
         }
    find any wrong?

    Implements() means it has implement the interfeace Recoder, but u printf
    "element does not implement". Are u kidding me?


    在 2015年12月8日星期二 UTC+8下午9:19:36,benm....@gmail.com写道:
    Hi,

    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 8, '15 at 1:19p
activeDec 8, '15 at 1:40p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase