{"record":{"id":"3b91c84a205dc66f","repo":"micro/go-micro","slug":"failed-to-write-v-is-not-type-of-byte-or-by-3b91c8","errorCode":null,"errorMessage":"failed to write: %v is not type of *[]byte or []byte","messagePattern":"failed to write: (.+?) is not type of \\*\\[\\]byte or \\[\\]byte","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codec/text/text.go","lineNumber":59,"sourceCode":"\n\treturn nil\n}\n\nfunc (c *Codec) Write(m *codec.Message, b interface{}) error {\n\tvar v []byte\n\tswitch ve := b.(type) {\n\tcase *Frame:\n\t\tv = ve.Data\n\tcase *[]byte:\n\t\tv = *ve\n\tcase *string:\n\t\tv = []byte(*ve)\n\tcase string:\n\t\tv = []byte(ve)\n\tcase []byte:\n\t\tv = ve\n\tdefault:\n\t\treturn fmt.Errorf(\"failed to write: %v is not type of *[]byte or []byte\", b)\n\t}\n\t_, err := c.Conn.Write(v)\n\treturn err\n}\n\nfunc (c *Codec) Close() error {\n\treturn c.Conn.Close()\n}\n\nfunc (c *Codec) String() string {\n\treturn \"text\"\n}\n\nfunc NewCodec(c io.ReadWriteCloser) codec.Codec {\n\treturn &Codec{\n\t\tConn: c,\n\t}\n}","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/codec/text/text.go#L41-L77","documentation":"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.","triggerScenarios":"Calling text codec Write with a struct, map, proto.Message, or nil body while the text codec (content-type text/*) is selected.","commonSituations":"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.","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"],"exampleFix":"// before\nc.Write(msg, MyStruct{A: 1}) // failed to write\n// after\ndata, _ := json.Marshal(MyStruct{A: 1})\nc.Write(msg, &text.Frame{Data: data})","handlingStrategy":"type-guard","validationCode":"func writableBody(b interface{}) ([]byte, bool) {\n    switch v := b.(type) {\n    case *text.Frame:\n        return v.Data, true\n    case *[]byte:\n        return *v, true\n    case *string:\n        return []byte(*v), true\n    case string:\n        return []byte(v), true\n    case []byte:\n        return v, true\n    }\n    return nil, false\n}","typeGuard":"func isTextWritable(b interface{}) bool {\n    switch b.(type) {\n    case *text.Frame, *[]byte, *string, string, []byte:\n        return true\n    default:\n        return false\n    }\n}","tryCatchPattern":null,"preventionTips":["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"],"tags":["codec","text","type-mismatch","go"],"backgroundTag":"codec-body-type-mismatch","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}