gofiber/fiber · critical

fasthttp.Client must not be nil

Error message

fasthttp.Client must not be nil

What it means

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.

Source

Thrown at client/client.go:936

	defaultUserAgent = "fiber"
)

func init() {
	defaultClient.Store(New())
}

// New creates and returns a new Client object.
func New() *Client {
	// Follow-up performance optimizations:
	// Try to use a pool to reduce the memory allocation cost for the Fiber client and the fasthttp client.
	// If possible, also consider pooling other structs (e.g., request headers, cookies, query parameters, path parameters).
	return NewWithClient(&fasthttp.Client{})
}

// NewWithClient creates and returns a new Client object from an existing fasthttp.Client.
func NewWithClient(c *fasthttp.Client) *Client {
	if c == nil {
		panic("fasthttp.Client must not be nil")
	}
	return newClient(newStandardClientTransport(c))
}

// NewWithHostClient creates and returns a new Client object from an existing fasthttp.HostClient.
func NewWithHostClient(c *fasthttp.HostClient) *Client {
	if c == nil {
		panic("fasthttp.HostClient must not be nil")
	}
	return newClient(newHostClientTransport(c))
}

// NewWithLBClient creates and returns a new Client object from an existing fasthttp.LBClient.
func NewWithLBClient(c *fasthttp.LBClient) *Client {
	if c == nil {
		panic("fasthttp.LBClient must not be nil")
	}
	return newClient(newLBClientTransport(c))

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Pass an initialized *fasthttp.Client: client.NewWithClient(&fasthttp.Client{}).
  2. Use client.New() if you want a default client without supplying one.
  3. Guard the call: if fc != nil { c = client.NewWithClient(fc) } else { c = client.New() }.
  4. Fix the factory/builder so it never returns a nil *fasthttp.Client.

Example fix

// before: uninitialized client
var fc *fasthttp.Client
c := client.NewWithClient(fc)

// after: always initialize
c := client.NewWithClient(&fasthttp.Client{
    ReadTimeout: 5 * time.Second,
})
// or simply use the default:
c := client.New()
Defensive patterns

Strategy: validation

Validate before calling

if fc == nil {
    fc = &fasthttp.Client{}
}
c := client.NewWithClient(fc)

Type guard

func isNilClient(c *fasthttp.Client) bool { return c == nil }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/5ec1042e31a80d70.json. Report an issue: GitHub.