{"record":{"id":"c112fb4561b3751e","repo":"gofiber/fiber","slug":"fasthttp-hostclient-must-not-be-nil","errorCode":null,"errorMessage":"fasthttp.HostClient must not be nil","messagePattern":"fasthttp\\.HostClient must not be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"client/client.go","lineNumber":944,"sourceCode":"func 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))\n}\n\nfunc newClient(transport httpClientTransport) *Client {\n\treturn &Client{\n\t\ttransport: transport,\n\t\theader: &Header{\n\t\t\tRequestHeader: &fasthttp.RequestHeader{},\n\t\t},","sourceCodeStart":926,"sourceCodeEnd":962,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/client/client.go#L926-L962","documentation":"NewWithHostClient wraps a *fasthttp.HostClient (single-host client) into a Fiber client. A nil HostClient would crash on first request, so the constructor panics at construction time. Prefer this over NewWithClient when you talk to one host and want connection reuse tuning.","triggerScenarios":"Calling fiber.NewWithHostClient(nil) or passing a *fasthttp.HostClient that was conditionally constructed and ended up nil.","commonSituations":"Building the HostClient from config where the config block was empty; helper that returns (*HostClient, error) where the error was ignored and the pointer is nil; runtime env missing the host setting.","solutions":["Construct the HostClient explicitly: &fasthttp.HostClient{Addr: \"example.com:443\"} and pass it non-nil.","Check the error from any builder helper before passing its result.","Default to fiber.New() when the host is unknown."],"exampleFix":"// before\nhc, _ := buildHostClient(cfg) // ignored error, hc is nil\nclient := fiber.NewWithHostClient(hc)\n\n// after\nhc, err := buildHostClient(cfg)\nif err != nil { return err }\nclient := fiber.NewWithHostClient(hc)","handlingStrategy":"validation","validationCode":"func newWithHostClientSafe(c *fasthttp.HostClient) (*fiber.Client, error) {\n    if c == nil { return nil, errors.New(\"HostClient is nil\") }\n    return fiber.NewWithHostClient(c), nil\n}","typeGuard":null,"tryCatchPattern":"func safeNewWithHostClient(c *fasthttp.HostClient) (cl *fiber.Client, err error) {\n    defer func() {\n        if r := recover(); r != nil { err = fmt.Errorf(\"%v\", r) }\n    }()\n    cl = fiber.NewWithHostClient(c)\n    return cl, nil\n}","preventionTips":["Always check the error from any HostClient builder helper.","Validate the Addr field is set before constructing the HostClient.","Default to fiber.New() when the target host is unknown."],"tags":["client","nil-guard","constructor","hostclient"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}