Arrays 检查包含数组字段的空结构

Arrays 检查包含数组字段的空结构,arrays,go,struct,slice,Arrays,Go,Struct,Slice,我有一个嵌套(非嵌入)结构,其中一些字段类型是数组 如何检查此结构的实例是否为空?(不使用迭代!!) 请注意,不能通过空实例使用StructIns==(Struct{})!此代码有以下错误: 无效操作:user==model.user literal(无法比较包含model.Configs的结构) user.Configs.TspConfigs: type TspConfigs struct { Flights []Flights `form:"flights" json:"

我有一个嵌套(非嵌入)结构,其中一些字段类型是数组

如何检查此结构的实例是否为空?(不使用迭代!!)

请注意,不能通过空实例使用
StructIns==(Struct{})
!此代码有以下错误:

无效操作:user==model.user literal(无法比较包含model.Configs的结构)

user.Configs.TspConfigs:

type TspConfigs struct {
    Flights     []Flights   `form:"flights" json:"flights"`
    Tours       []Tours     `form:"tours" json:"tours"`
    Insurances  []Insurances`form:"insurances" json:"insurances"`
    Hotels      []Hotels    `form:"hotels" json:"hotels"`
}
那些是,不是。强调这一点很重要,因为数组是可比较的,而切片则不是。看见由于切片是不可比较的,因此由切片组成的结构(具有切片类型的字段的结构)也是不可比较的

你可以用这个。例如:

type Foo struct {
    A []int
    B []string
}

f := Foo{}
fmt.Println("Zero:", reflect.DeepEqual(f, Foo{}))
f.A = []int{1}
fmt.Println("Zero:", reflect.DeepEqual(f, Foo{}))
输出(在上尝试):


迭代是唯一的可能性。我使用了github的一个名为deep或deep的包,它有一个类似于reflect.DeepEqual()的方法,该方法还标识第一个非相等组件。
Zero: true
Zero: false