go-redis/redis · error

redis: can't marshal %T (implement encoding.BinaryMarshaler)

Error message

redis: can't marshal %T (implement encoding.BinaryMarshaler)

What it means

Error "redis: can't marshal %T (implement encoding.BinaryMarshaler)" thrown in go-redis/redis.

Source

Thrown at internal/proto/writer.go:197

		w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano)
		return w.bytes(w.numBuf)
	case time.Duration:
		return w.int(v.Nanoseconds())
	case *time.Duration:
		if v == nil {
			return w.int(0)
		}
		return w.int(v.Nanoseconds())
	case encoding.BinaryMarshaler:
		b, err := v.MarshalBinary()
		if err != nil {
			return err
		}
		return w.bytes(b)
	case net.IP:
		return w.bytes(v)
	default:
		return fmt.Errorf(
			"redis: can't marshal %T (implement encoding.BinaryMarshaler)", v)
	}
}

func (w *Writer) bytes(b []byte) error {
	if err := w.WriteByte(RespString); err != nil {
		return err
	}

	if err := w.writeLen(len(b)); err != nil {
		return err
	}

	if _, err := w.Write(b); err != nil {
		return err
	}

	return w.crlf()

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Implement encoding.BinaryMarshaler on the argument type
  2. Convert the value to string/[]byte/int before passing it as a command argument

When it happens

Trigger: Thrown at internal/proto/writer.go:197 when the library encounters an invalid state.

Common situations: Marshal custom argument types explicitly at the call site.


AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06). Data as JSON: /data/errors/76c942349eeb6275.json. Report an issue: GitHub.