Does Go是否有类似Python的in
关键字?我想检查一个值是否在列表中。检查一个值是否在列表中
例如在Python:
x = 'red'
if x in ['red', 'green', 'yellow', 'blue']:
print "found"
else:
print "not found"
在围棋,我想出了使用设定的成语,但我不认为这是理想的,因为我必须指定,我不是用int值。
x := "red"
valid := map[string]int{"red": 0, "green": 0,"yellow": 0, "blue": 0}
if _, ok := valid[x]; ok {
fmt.Println("found")
} else {
fmt.Println("not found")
}
我知道有一个in
关键字可能与泛型有关。有没有办法做到这一点使用去生成或什么?
另请参阅[http://stackoverflow.com/questions/15323767/how-to-if-x-in-array-in-golang](http://stackoverflow.com/questions/15323767/how-to- if-x-in-array-in-golang) – IamNaN
为什么它没有被作为重复关闭? –