{"id":"5ec1042e31a80d70","repo":"gofiber/fiber","slug":"fasthttp-client-must-not-be-nil","errorCode":null,"errorMessage":"fasthttp.Client must not be nil","messagePattern":"fasthttp\\.Client must not be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"client/client.go","lineNumber":936,"sourceCode":"\tdefaultUserAgent = \"fiber\"\n)\n\nfunc init() {\n\tdefaultClient.Store(New())\n}\n\n// New creates and returns a new Client object.\nfunc New() *Client {\n\t// Follow-up performance optimizations:\n\t// Try to use a pool to reduce the memory allocation cost for the Fiber client and the fasthttp client.\n\t// If possible, also consider pooling other structs (e.g., request headers, cookies, query parameters, path parameters).\n\treturn NewWithClient(&fasthttp.Client{})\n}\n\n// NewWithClient creates and returns a new Client object from an existing fasthttp.Client.\nfunc NewWithClient(c *fasthttp.Client) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.Client must not be nil\")\n\t}\n\treturn newClient(newStandardClientTransport(c))\n}\n\n// NewWithHostClient creates and returns a new Client object from an existing fasthttp.HostClient.\nfunc NewWithHostClient(c *fasthttp.HostClient) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.HostClient must not be nil\")\n\t}\n\treturn newClient(newHostClientTransport(c))\n}\n\n// NewWithLBClient creates and returns a new Client object from an existing fasthttp.LBClient.\nfunc NewWithLBClient(c *fasthttp.LBClient) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.LBClient must not be nil\")\n\t}\n\treturn newClient(newLBClientTransport(c))","sourceCodeStart":918,"sourceCodeEnd":954,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/client.go#L918-L954","documentation":"Panicked by NewWithClient (client/client.go:936) when the provided *fasthttp.Client is nil. The Fiber client wraps a fasthttp.Client to perform outbound HTTP; a nil client would nil-pointer-dereference on the first request, so the constructor validates up front and panics with a clear message.","triggerScenarios":"Calling client.NewWithClient(nil) — typically when a *fasthttp.Client variable was declared but never initialized, a factory returned nil on misconfiguration, or code conditionally passes nil.","commonSituations":"Dependency injection where the client wasn't wired; refactor that removed the fasthttp.Client initialization; tests that pass nil expecting a default; conditional construction that leaves the client nil.","solutions":["Pass an initialized *fasthttp.Client: client.NewWithClient(&fasthttp.Client{}).","Use client.New() if you want a default client without supplying one.","Guard the call: if fc != nil { c = client.NewWithClient(fc) } else { c = client.New() }.","Fix the factory/builder so it never returns a nil *fasthttp.Client."],"exampleFix":"// before: uninitialized client\nvar fc *fasthttp.Client\nc := client.NewWithClient(fc)\n\n// after: always initialize\nc := client.NewWithClient(&fasthttp.Client{\n    ReadTimeout: 5 * time.Second,\n})\n// or simply use the default:\nc := client.New()","handlingStrategy":"validation","validationCode":"if fc == nil {\n    fc = &fasthttp.Client{}\n}\nc := client.NewWithClient(fc)","typeGuard":"func isNilClient(c *fasthttp.Client) bool { return c == nil }","tryCatchPattern":null,"preventionTips":["Always initialize *fasthttp.Client before passing it.","Prefer client.New() when you do not need a custom fasthttp client.","In DI/factory code, return an error instead of nil for missing clients."],"tags":["client","fasthttp","nil-check","panic","http-client"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}