This question already has an answer here:
In Python I can do something like this:
aModel = None
models = somefunction()
for model in models:
if model.cool is False and model.somenumber > -5:
aModel = model
break
if aModel:
print("We found a model we like!!")
I'm trying to do the same thing in Golang. After initialization, a variable of some struct already IS a struct and already HAS values though (such as false
for a bool
var, and 0
for an int
var).
So considering the following code:
type SomeModel struct {
cool bool
somenumber int
}
func main() {
somemodels = somefunction()
var aModel SomeModel
for _, v := range somemodels {
fmt.Println(v)
if (v.cool == false && v.somenumber > -5) {
aModel = v
}
}
fmt.Println(aModel)
}
If this prints out {false 0}
, how can I know whether this is a model I found in the slice, or if it's the default model I set before the loop?
I can of course set another variable like foundamodel := false
and set that to true if I found something, but that doesn't really seem like the obvious way to go.
Or is it in Go?
</div>