{"record":{"id":"ce935a0480efb1e9","repo":"micro/go-micro","slug":"failed-to-cast-to-bool","errorCode":null,"errorMessage":"failed to cast to bool","messagePattern":"failed to cast to bool","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/rpc_client.go","lineNumber":718,"sourceCode":"\n\t\tif err = cf(b).Write(&codec.Message{\n\t\t\tTarget: topic,\n\t\t\tType:   codec.Event,\n\t\t\tHeader: map[string]string{\n\t\t\t\theaders.ID:      id,\n\t\t\t\theaders.Message: msg.Topic(),\n\t\t\t},\n\t\t}, msg.Payload()); err != nil {\n\t\t\treturn merrors.InternalServerError(packageID, err.Error())\n\t\t}\n\n\t\t// set the body\n\t\tbody = b.Bytes()\n\t}\n\n\tl, ok := r.once.Load().(bool)\n\tif !ok {\n\t\treturn fmt.Errorf(\"failed to cast to bool\")\n\t}\n\n\tif !l {\n\t\tif err = r.opts.Broker.Connect(); err != nil {\n\t\t\treturn merrors.InternalServerError(packageID, err.Error())\n\t\t}\n\n\t\tr.once.Store(true)\n\t}\n\n\treturn r.opts.Broker.Publish(topic, &broker.Message{\n\t\tHeader: metadata,\n\t\tBody:   body,\n\t}, broker.PublishContext(options.Context))\n}\n\nfunc (r *rpcClient) NewMessage(topic string, message interface{}, opts ...MessageOption) Message {\n\treturn newMessage(topic, message, r.opts.ContentType, opts...)","sourceCodeStart":700,"sourceCodeEnd":736,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/client/rpc_client.go#L700-L736","documentation":"Publish reads a lazily-initialized boolean from the client's atomic r.once (sync/atomic Value) and asserts it to bool. If the stored value is not a bool — meaning the client's internal init never ran or stored something unexpected — Publish returns this error instead of proceeding to broker connect/publish.","triggerScenarios":"Calling Publish on an rpcClient that was constructed but never initialized (Init/Connect path that populates r.once not executed), or a zero-value/misconstructed rpcClient.","commonSituations":"Using client.Publish without first calling client.Init(...) in custom wiring; building an rpcClient manually instead of via the constructor; concurrency where Publish races the init in unusual embedding setups.","solutions":["Call client.Init(opts...) before using Publish","Construct the client through the library constructor (newClient/rpcClient built with proper defaults) rather than zero-value struct literals","Check that your service start path initializes the client before publishers run","Upgrade the framework version if your init order looks correct — some versions fixed once/Store races"],"exampleFix":"// before\ncli := service.Client()\ncli.Publish(ctx, topic, msg)\n// after\ncli := service.Client()\nif err := cli.Init(); err != nil { log.Fatal(err) }\ncli.Publish(ctx, topic, msg)","handlingStrategy":"try-catch","validationCode":"// ensure initialization before publishing\nif err := client.Init(clientOpts...); err != nil {\n    return err\n}","typeGuard":null,"tryCatchPattern":"if err := client.Publish(ctx, topic, msg); err != nil {\n    if strings.Contains(err.Error(), \"failed to cast to bool\") {\n        // client was not initialized: call Init and retry\n    }\n}","preventionTips":["Always call client.Init before Publish","Construct clients via library constructors, not struct literals","Sequence service startup: Init -> Connect -> Publish","Log init failures instead of proceeding with a half-built client"],"tags":["rpc","publish","initialization","broker"],"backgroundTag":"client-not-initialized","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}