{"record":{"id":"46729ca2b0c642f5","repo":"panjf2000/ants","slug":"must-provide-function-for-pool","errorCode":null,"errorMessage":"must provide function for pool","messagePattern":"must provide function for pool","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ants.go","lineNumber":64,"sourceCode":"const (\n\t// DefaultAntsPoolSize is the default capacity for a default goroutine pool.\n\tDefaultAntsPoolSize = math.MaxInt32\n\n\t// DefaultCleanIntervalTime is the interval time to clean up goroutines.\n\tDefaultCleanIntervalTime = time.Second\n)\n\nconst (\n\t// OPENED represents that the pool is opened.\n\tOPENED = iota\n\n\t// CLOSED represents that the pool is closed.\n\tCLOSED\n)\n\nvar (\n\t// ErrLackPoolFunc will be returned when invokers don't provide function for pool.\n\tErrLackPoolFunc = errors.New(\"must provide function for pool\")\n\n\t// ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines.\n\tErrInvalidPoolExpiry = errors.New(\"invalid expiry for pool\")\n\n\t// ErrPoolClosed will be returned when submitting task to a closed pool.\n\tErrPoolClosed = errors.New(\"this pool has been closed\")\n\n\t// ErrPoolOverload will be returned when the pool is full and no workers available.\n\tErrPoolOverload = errors.New(\"too many goroutines blocked on submit or Nonblocking is set\")\n\n\t// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.\n\tErrInvalidPreAllocSize = errors.New(\"can not set up a negative capacity under PreAlloc mode\")\n\n\t// ErrTimeout will be returned after the operations timed out.\n\tErrTimeout = errors.New(\"operation timed out\")\n\n\t// ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index.\n\tErrInvalidPoolIndex = errors.New(\"invalid pool index\")","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/panjf2000/ants/blob/107e37678122b00e1b365636ce4f80623ba41579/ants.go#L46-L82","documentation":"ErrLackPoolFunc is returned when a caller creates a worker pool without providing a task function. Pools created via NewPoolWithFunc and NewPoolWithFuncGeneric bind a single function at construction time, so a nil (or nil-typed) function argument makes the pool unusable and construction fails with this sentinel. It is a constructor-time validation error in ants.go:64.","triggerScenarios":"Calling ants.NewPoolWithFunc(size, nil) or ants.NewPoolWithFuncGeneric(size, nil); also passing a declared-but-unassigned typed nil like `var fn func(i int); NewPoolWithFuncGeneric(1, fn)`.","commonSituations":"Declaring the task function variable but forgetting to assign it before pool construction; refactoring that removes a function argument; conditionally built functions that end up nil.","solutions":["Pass a non-nil function matching the required signature (e.g. func(i int)) to NewPoolWithFunc / NewPoolWithFuncGeneric.","If the task function is variable, check for nil before constructing the pool and return a descriptive error.","If you submit different closures per task, use ants.NewPool and pool.Submit(func) instead of the WithFunc variants."],"exampleFix":"// before\nvar fn func(i int)\npool, err := ants.NewPoolWithFuncGeneric(10, fn)\n// after\npoolFunc := func(i int) { process(i) }\npool, err := ants.NewPoolWithFuncGeneric(10, poolFunc)","handlingStrategy":"validation","validationCode":"if fn == nil {\n    return fmt.Errorf(\"pool task function must not be nil\")\n}\npool, err := ants.NewPoolWithFuncGeneric(size, fn)","typeGuard":"func isNilFunc(fn any) bool {\n    if fn == nil {\n        return true\n    }\n    v := reflect.ValueOf(fn)\n    return v.Kind() == reflect.Func && v.IsNil()\n}","tryCatchPattern":null,"preventionTips":["Always assign the task function before constructing the pool.","Add a nil check on function parameters at the boundary where they are configured.","Prefer NewPool + Submit when tasks vary per call; reserve WithFunc pools for one fixed function."],"tags":["go","validation","nil-argument","constructor"],"backgroundTag":"null-argument","analyzedSha":"107e37678122b00e1b365636ce4f80623ba41579","analyzedAt":"2026-09-06T13:28:28.492Z","contentChangedAt":"2026-09-06T13:28:28.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}