{"record":{"id":"fd538fd34ebc6b36","repo":"dianping/cat","slug":"only-1-config-can-be-specified-while-initializing","errorCode":null,"errorMessage":"Only 1 config can be specified while initializing cat.","messagePattern":"Only 1 config can be specified while initializing cat\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/go/gocat/cat.go","lineNumber":55,"sourceCode":"\t\t1,\n\t\t0,\n\t}\n}\n\nfunc DefaultConfigForCat2() Config {\n\treturn Config{\n\t\tENCODER_TEXT,\n\t\t1,\n\t\t0,\n\t\t0,\n\t}\n}\n\n\nfunc Init(domain string, configs ...Config) {\n\tvar config Config;\n\tif len(configs) > 1 {\n\t\tpanic(\"Only 1 config can be specified while initializing cat.\")\n\t} else if len(configs) == 1 {\n\t\tconfig = configs[0]\n\t} else {\n\t\tconfig = DefaultConfig()\n\t}\n\n\tccat.InitWithConfig(domain, ccat.BuildConfig(\n\t\tconfig.EncoderType,\n\t\tconfig.EnableHeartbeat,\n\t\tconfig.EnableSampling,\n\t\tconfig.EnableDebugLog,\n\t))\n\tgo ccat.Background()\n}\n\nfunc Shutdown() {\n\tccat.Shutdown()\n}","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/lib/go/gocat/cat.go#L37-L73","documentation":"gocat's Init panics when more than one Config value is passed via its variadic parameter. The API is designed so callers either pass no config (defaults are used), or exactly one Config; multiple configs are ambiguous so the library aborts immediately rather than guessing. The panic occurs before ccat.InitWithConfig is ever called, so nothing is initialized when it fires.","triggerScenarios":"Calling gocat.Init(domain, cfg1, cfg2) with two or more Config arguments, e.g. trying to combine a text-encoder config and a heartbeat-enabled config instead of building a single Config with all fields set.","commonSituations":"New Go users assume Configs are composable and pass several to enable multiple options; refactoring code where configs from different call sites got concatenated into one Init call; copying sample code that built a Config and appending another 'just in case'.","solutions":["Pass at most one Config: merge the desired options into a single Config value before calling Init","Or call Init(domain) with no config to accept DefaultConfig() (text encoder, heartbeat on, sampling/debug off)","Use recover() around Init during development if the config count comes from dynamic code"],"exampleFix":"// before\ngocat.Init(\"mydomain\", gocat.Config{gocat.ENCODER_TEXT, 1, 0, 0},\n    gocat.Config{gocat.ENCODER_BINARY, 0, 1, 0})\n\n// after\ngocat.Init(\"mydomain\", gocat.Config{\n    EncoderType:     gocat.ENCODER_TEXT,\n    EnableHeartbeat: 1,\n    EnableSampling:  0,\n    EnableDebugLog:  0,\n})","handlingStrategy":"validation","validationCode":"func safeInit(domain string, configs ...gocat.Config) {\n    if len(configs) > 1 {\n        cfg := configs[0] // merge or pick explicitly instead of panicking\n        log.Printf(\"gocat: ignoring extra configs (%d passed, 1 allowed)\", len(configs))\n        gocat.Init(domain, cfg)\n        return\n    }\n    gocat.Init(domain, configs...)\n}","typeGuard":"func isValidInitArgs(domain string, configs []gocat.Config) bool {\n    return domain != \"\" && len(configs) <= 1\n}","tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        log.Fatalf(\"gocat init failed: %v\", r)\n    }\n}()\ngocat.Init(\"mydomain\", cfg1, cfg2)","preventionTips":["Build one Config struct with all desired fields instead of passing multiple Configs","Call gocat.Init exactly once at process start, from a single init function","Remember Init with zero configs is valid and applies DefaultConfig()"],"tags":["cat","go","gocat","config","panic","api-misuse"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}