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 text codec's Write accepts only *Frame, *[]byte, *string, string, and []byte payloads; anything else returns this error. The codec writes bytes verbatim to the connection and cannot serialize structured values.
Source
Thrown at codec/text/text.go:59
return nil
}
func (c *Codec) Write(m *codec.Message, b interface{}) error {
var v []byte
switch ve := b.(type) {
case *Frame:
v = ve.Data
case *[]byte:
v = *ve
case *string:
v = []byte(*ve)
case string:
v = []byte(ve)
case []byte:
v = ve
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 "text"
}
func NewCodec(c io.ReadWriteCloser) codec.Codec {
return &Codec{
Conn: c,
}
}View on GitHub (pinned to 24529f1404)
Solutions
- Marshal the value yourself (json.Marshal or proto.Marshal) and pass the resulting []byte or *Frame
- Use *text.Frame{Data: payload} to make raw intent explicit
- Switch the topic/service content type to json or proto so serialization happens for you
- For handlers, return *[]byte or *Frame rather than typed structs when using the text codec
Example fix
// before
c.Write(msg, MyStruct{A: 1}) // failed to write
// after
data, _ := json.Marshal(MyStruct{A: 1})
c.Write(msg, &text.Frame{Data: data}) Defensive patterns
Strategy: type-guard
Validate before calling
func writableBody(b interface{}) ([]byte, bool) {
switch v := b.(type) {
case *text.Frame:
return v.Data, true
case *[]byte:
return *v, true
case *string:
return []byte(*v), true
case string:
return []byte(v), true
case []byte:
return v, true
}
return nil, false
} Type guard
func isTextWritable(b interface{}) bool {
switch b.(type) {
case *text.Frame, *[]byte, *string, string, []byte:
return true
default:
return false
}
} Prevention
- Marshal structs to JSON/bytes before publishing with the text codec
- Prefer *text.Frame for explicit raw payload intent
- Use json/proto codecs when you want automatic serialization
When it happens
Trigger: Calling text codec Write with a struct, map, proto.Message, or nil body while the text codec (content-type text/*) is selected.
Common situations: Publishing typed events over a text/* topic; returning structured responses from handlers when the client negotiates text; forgetting to marshal a struct to JSON/bytes before publishing with the raw/text codec.
Related errors
- failed to read body: %v is not type of *[]byte
- ErrInvalidMessage
- unknown request path
- unsupported Content-Type
- failed to read body: %v is not type of *[]byte
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/3b91c84a205dc66f.
Report an issue: GitHub.