{"record":{"id":"71de6414ac63e0b0","repo":"gofiber/fiber","slug":"binder-map-destination-is-nil-and-cannot-be-initi","errorCode":null,"errorMessage":"binder: map destination is nil and cannot be initialized","messagePattern":"binder: map destination is nil and cannot be initialized","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binder/binder.go","lineNumber":21,"sourceCode":"import (\n\t\"errors\"\n\t\"sync\"\n)\n\nconst (\n\tbindingURI        = \"uri\"\n\tbindingForm       = \"form\"\n\tbindingQuery      = \"query\"\n\tbindingHeader     = \"header\"\n\tbindingRespHeader = \"respHeader\"\n\tbindingCookie     = \"cookie\"\n)\n\n// Binder errors\nvar (\n\tErrSuitableContentNotFound = errors.New(\"binder: suitable content not found to parse body\")\n\tErrMapNotConvertible       = errors.New(\"binder: map is not convertible to map[string]string or map[string][]string\")\n\tErrMapNilDestination       = errors.New(\"binder: map destination is nil and cannot be initialized\")\n\tErrInvalidDestinationValue = errors.New(\"binder: invalid destination value\")\n\tErrUnmatchedBrackets       = errors.New(\"unmatched brackets\")\n)\n\nvar errPoolTypeAssertion = errors.New(\"failed to type-assert to T\")\n\nvar HeaderBinderPool = sync.Pool{\n\tNew: func() any {\n\t\treturn &HeaderBinding{}\n\t},\n}\n\nvar RespHeaderBinderPool = sync.Pool{\n\tNew: func() any {\n\t\treturn &RespHeaderBinding{}\n\t},\n}\n","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/binder/binder.go#L3-L39","documentation":"Returned by parseToMap (binder/mapping.go:144) when the destination is a nil map whose reflect.Value cannot be Set. The binder wants to initialize the map with reflect.MakeMap, but if the caller passed the map by value (not through a pointer) the value is not addressable and cannot be mutated.","triggerScenarios":"Passing a nil map directly rather than a pointer to a map: parseToMap(reflect.ValueOf(make-nil-map), data) where the Value was obtained from a non-pointer. Also when binding into a map field behind an interface that is not settable.","commonSituations":"Calling c.Bind().Query(m) with `var m map[string]string` (value, not pointer) — Go passes the map by value, reflect sees it as unsettable; binding into a nil map stored in an interface{} variable.","solutions":["Pass a pointer to the map: c.Bind().Query(&m) so reflect can allocate and assign the map.","Pre-initialize the map with make so the nil check is bypassed: m := make(map[string]string) before binding.","Ensure the destination is obtained via reflect.ValueOf(out).Elem() from a pointer (the normal Bind path does this; the error typically comes from direct parseToMap use)."],"exampleFix":"// before\nvar m map[string]string\nc.Bind().Query(m) // or parseToMap(reflect.ValueOf(m), data) -> ErrMapNilDestination\n\n// after\nvar m map[string]string\nc.Bind().Query(&m) // pointer lets the binder allocate the map","handlingStrategy":"validation","validationCode":"// Ensure maps are passed by pointer and/or pre-allocated\nfunc prepareMapDest[T any]() (*map[string]string, error) {\n    m := make(map[string]string)\n    return &m, nil\n}","typeGuard":"func isSettableMapPtr(v any) bool {\n    rv := reflect.ValueOf(v)\n    return rv.Kind() == reflect.Ptr && rv.Elem().Kind() == reflect.Map\n}","tryCatchPattern":"if err := c.Bind().Query(&m); err != nil {\n    if errors.Is(err, binder.ErrMapNilDestination) {\n        m = make(map[string]string) // pre-allocate and retry\n        err = c.Bind().Query(&m)\n    }\n    if err != nil { return err }\n}","preventionTips":["Always pass a pointer to the destination: &m, not m.","Pre-allocate maps with make so the nil path is never hit.","Use the public Bind.* methods rather than calling parseToMap directly."],"tags":["binding","reflection","map","fiber"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}