micro/go-micro · error
failed to write: %v is not type of *[]byte or []byte
Error message
failed to write: %v is not type of *[]byte or []byte
What it means
The bytes codec's Write method only accepts bodies of type *Frame, *[]byte, or []byte, since it writes raw bytes straight to the connection. Any other type fails with this error before any network write occurs, so the connection is left unmodified.
Source
Thrown at codec/bytes/bytes.go:53
v.Data = buf
default:
return fmt.Errorf("failed to read body: %v is not type of *[]byte", b)
}
return nil
}
func (c *Codec) Write(m *codec.Message, b interface{}) error {
var v []byte
switch vb := b.(type) {
case *Frame:
v = vb.Data
case *[]byte:
v = *vb
case []byte:
v = vb
default:
return fmt.Errorf("failed to write: %v is not type of *[]byte or []byte", b)
}
_, err := c.Conn.Write(v)
return err
}
func (c *Codec) Close() error {
return c.Conn.Close()
}
func (c *Codec) String() string {
return "bytes"
}
func NewCodec(c io.ReadWriteCloser) codec.Codec {
return &Codec{
Conn: c,
}
}View on GitHub (pinned to 24529f1404)
Solutions
- Marshal your payload to []byte before writing (json.Marshal or []byte(str)).
- Pass *Frame or *[]byte if the caller provides a pointer.
- Register the correct codec (jsonrpc/grpc) for structured messages.
- Inspect the call site (e.g. serveReq) to see what body type it builds and align it with the codec.
Example fix
// before
c.Write(m, "hello")
// after
c.Write(m, []byte("hello")) Defensive patterns
Strategy: type-guard
Validate before calling
func isWritableBytes(b interface{}) bool {
switch b.(type) {
case []byte, *[]byte, *Frame:
return true
}
return false
} Type guard
func toBytes(b interface{}) ([]byte, bool) {
switch v := b.(type) {
case []byte:
return v, true
case *[]byte:
return *v, true
case *Frame:
return v.Data, true
}
return nil, false
} Try / catch
body, ok := toBytes(payload)
if !ok {
body, _ = json.Marshal(payload) // marshal structured payloads first
}
if err := c.Write(m, body); err != nil { return err } Prevention
- Marshal strings/structs to []byte before writing with the bytes codec.
- Confirm the codec type before assuming structured writes work.
- Keep a helper that normalizes payloads to []byte at the transport boundary.
- Cover the write path with a codec-specific integration test.
When it happens
Trigger: Calling codec.Write (via serveReq or client send path) on a bytes.Codec with a body that is not []byte, *[]byte, or *Frame, e.g. a string or struct.
Common situations: Publishing/requesting with a payload that was built for the JSON codec; handlers marshaling to string instead of []byte; framework misconfiguration where bytes codec is registered but request/response bodies are structured.
Related errors
- failed to read body: %v is not type of *[]byte
- failed to marshal: %v is not type of *[]byte
- failed to unmarshal: %v is not type of *[]byte
- failed to read body: %v is not type of *[]byte
- failed to write: %v is not type of *[]byte or []byte
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/828c2d1c7f92fa08.
Report an issue: GitHub.