{"record":{"id":"8e419681e30c5d0c","repo":"JanDeDobbeleer/oh-my-posh","slug":"input-must-be-a-slice-or-array","errorCode":null,"errorMessage":"input must be a slice or array","messagePattern":"input must be a slice or array","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/template/random.go","lineNumber":14,"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":"The template function random(list) picks a random element from a list passed in a template. It uses reflection to inspect the argument and throws 'input must be a slice or array' when the value's Kind is neither slice nor array — e.g. a string, map, or scalar was passed.","triggerScenarios":"A template calls `random` with a non-list argument, such as a bare string, an int, a map, or a nil value produced by an unset template property.","commonSituations":"Typo in the template property name yielding nil; passing a comma-separated string instead of an actual slice; passing a map of options instead of a slice; calling random with no argument.","solutions":["Pass an actual slice/array to random in the template","Fix the template property name so it resolves to the intended list","Convert string input to a list before calling random","Guard the call with a conditional on the value being a non-empty list"],"exampleFix":"// before (template)\n{{ random \"a,b,c\" }}\n// after\n{{ random (list \"a\" \"b\" \"c\") }}","handlingStrategy":"type-guard","validationCode":"// in template logic, ensure the argument is a list\n{{ if and (not (eq (typeOf .Value) \"string\")) (gt (len .Value) 0) }}{{ random .Value }}{{ end }}","typeGuard":"func isList(v any) bool {\n    k := reflect.ValueOf(v).Kind()\n    return k == reflect.Slice || k == reflect.Array\n}","tryCatchPattern":null,"preventionTips":["Only pass slices/arrays to random","Fix property name typos that yield nil","Convert delimited strings to lists before calling random"],"tags":["template","reflection","type-error","go"],"backgroundTag":"invalid-argument-value","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}