{"record":{"id":"5ff04d602528afeb","repo":"JanDeDobbeleer/oh-my-posh","slug":"input-slice-or-array-is-empty","errorCode":null,"errorMessage":"input slice or array is empty","messagePattern":"input slice or array is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/template/random.go","lineNumber":18,"sourceCode":"package template\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"math/rand/v2\"\n\t\"reflect\"\n)\n\nfunc random(list any) (string, error) {\n\tv := reflect.ValueOf(list)\n\n\tif v.Kind() != reflect.Slice && v.Kind() != reflect.Array {\n\t\treturn \"\", errors.New(\"input must be a slice or array\")\n\t}\n\n\tif v.Len() == 0 {\n\t\treturn \"\", errors.New(\"input slice or array is empty\")\n\t}\n\n\treturn fmt.Sprintf(\"%v\", v.Index(rand.IntN(v.Len()))), nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/template/random.go#L1-L23","documentation":"After confirming the input is a slice or array, random() checks v.Len() == 0 and throws 'input slice or array is empty' because there is no element to pick. rand.IntN would panic on a zero length, so this guard converts it to an error.","triggerScenarios":"A template calls `random` with a slice/array value that exists but has zero elements — an empty list literal, an empty environment-derived list, or a list built by filtering that removed everything.","commonSituations":"An empty env var split into a list; a template property that is an initialized but unpopulated array; passing `list` with no elements; config where the list is populated only under conditions that didn't hold.","solutions":["Ensure the list passed to random contains at least one element","Add a fallback default element when the source list may be empty","Conditionally render the random call only when the list is non-empty","Fix the population logic (env var, property) that produced an empty list"],"exampleFix":"// before (template)\n{{ random .EmptyList }}\n// after\n{{ if gt (len .EmptyList) 0 }}{{ random .EmptyList }}{{ else }}default{{ end }}","handlingStrategy":"validation","validationCode":"// check length before calling random in a template\n{{ if gt (len .List) 0 }}{{ random .List }}{{ else }}default{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check len(list) > 0 before random","Provide a default value for possibly-empty lists","Verify env-derived lists are populated before rendering"],"tags":["template","reflection","empty-list","go"],"backgroundTag":"empty-collection","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}