micro/go-micro · error

failed to marshal: %v is not type of *[]byte

Error message

failed to marshal: %v is not type of *[]byte

What it means

The bytes codec Marshal only accepts a *[]byte; the value handed to it was any other type. This codec is a raw passthrough — it forwards your byte slice verbatim as the gRPC message body — so it cannot serialize structured values.

Source

Thrown at client/grpc/codec.go:98

	switch m := v.(type) {
	case proto.Message:
		return proto.Unmarshal(data, m)
	case protoiface.MessageV1:
		// #2333 compatible with etcd legacy proto.Message
		m2 := protoimpl.X.ProtoMessageV2Of(m)
		return proto.Unmarshal(data, m2)
	}
	return fmt.Errorf("failed to unmarshal: %v is not type of proto.Message", v)
}

func (protoCodec) Name() string {
	return "proto"
}

func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
	b, ok := v.(*[]byte)
	if !ok {
		return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v)
	}
	return *b, nil
}

func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
	b, ok := v.(*[]byte)
	if !ok {
		return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v)
	}
	*b = data
	return nil
}

func (bytesCodec) Name() string {
	return "bytes"
}

func (jsonCodec) Marshal(v interface{}) ([]byte, error) {

View on GitHub (pinned to 24529f1404)

Solutions

  1. Wrap the payload: pre-marshal to bytes and pass &payload
  2. Switch Content-Type to application/grpc+proto or +json for structured types
  3. Pass *raw.Frame if you intend raw framing
  4. Audit Publish/Call sites after any codec configuration change

Example fix

// before
client.Publish(ctx, topic, map[string]string{"k": "v"})
// after
b, _ := json.Marshal(map[string]string{"k": "v"})
client.Publish(ctx, topic, &b)
Defensive patterns

Strategy: type-guard

Validate before calling

if _, ok := payload.(*[]byte); !ok {
    return fmt.Errorf("bytes codec requires *[]byte, got %T", payload)
}

Type guard

func isBytePtr(v interface{}) bool {
    _, ok := v.(*[]byte)
    return ok
}

Try / catch

if err := client.Publish(ctx, topic, payload); err != nil {
    if strings.Contains(err.Error(), "is not type of *[]byte") {
        // marshal payload to bytes and pass a pointer
    }
}

Prevention

When it happens

Trigger: Using Content-Type application/grpc+bytes (or registering bytesCodec) and calling Publish/Call with a struct, string (not *[]byte), or map instead of a pointer to a byte slice.

Common situations: Selecting the bytes codec for efficiency but forgetting to pre-marshal payloads; passing a []byte (non-pointer) which also fails the *[]byte assertion; switching codecs without updating call sites.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/49f3f2e7682987f6. Report an issue: GitHub.