如何知道Golang中是否填写了空结构变量?

如何知道Golang中是否填写了空结构变量?,go,Go,在Python中,我可以执行以下操作: 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!!") 我正试图在戈朗做同样的事情。初始化后,某个结构的

在Python中,我可以执行以下操作:

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!!")
我正试图在戈朗做同样的事情。初始化后,某个结构的变量已经是一个结构并且已经有了值(例如
bool
var的
false
,以及
int
var的
0

因此,考虑到以下准则:

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)
}
如果打印出
{false 0}
,我如何知道这是我在切片中找到的模型,还是我在循环之前设置的默认模型

当然,我可以设置另一个变量,比如
foundamodel:=false
,如果我发现了什么,就将其设置为true,但这似乎不是一个显而易见的方法


还是在Go中?

您可以使用指针而不是具体值:

var aModel *SomeModel
for _, v := range somemodels {
    fmt.Println(v)
    if v.cool == false && v.somenumber > -5 {
        aModel = &v
        break
    }
}
if aModel != nil {
    fmt.Println(*aModel)
}