#encoding.BinaryUnmarshaler as type parameter

3 messages · Page 1 of 1 (latest)

stiff jetty
#

because it's a nil pointer.
why is it a nil pointer? if it's to be returned, new(T) and return it

stiff jetty
#

What code do you actually write... you also don't constraint it... so for instance this would/should work

func Decode[T encoding.BinaryUnmarshaler](data []byte) (T, error) {
    var t T
    if err := t.UnmarshalBinary(data); err != nil {
        return t, err
    }
    return t, nil
}
#

actually not quite... but if you have an interface why do you need generic?