首页 > 解决方案 > Check for empty inline struct

问题描述

Can an inline struct in Go be checked for emptiness?

The following example checks p, which is of type Person for emptiness.

It also defines the inline struct i. (How) can i be checked for emptiness?

package main

import (
    "fmt"
)

type Person struct {
    name string
}

func main() {
    // Types can be checked for emptiness.
    p := Person{}
    if (p == Person{}) {
        fmt.Println("Empty!")
    }

    // But: how to check for an empty inline struct?
    // I.e. how to check, if i is empty?
    var i struct {
        value int
    }
    fmt.Println(i) // Required, or compiler will complain about unused 'i'.
}

标签: go

解决方案


推荐阅读