Using Go, what is the idiomatic way to check if a string is one of many predefined values?
- User
Using Go, what is the idiomatic way to check if a string is one of many predefined values?
Assistant
In Go, there are two main idiomatic approaches depending on your use case:
Switch Statement (Most Common)
For a small number of predefined values, a switch statement is the most idiomatic:
func isValidStatus(status string) bool { switch status { case "active", "inactive", "pending", "suspended": return true default: return false } }Map as a Set (For Larger Sets)
For larger sets of values or when you need O(1) lookup performance, use a map:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.