Home  >  Article  >  Backend Development  >  How to determine whether a golang structure is empty

How to determine whether a golang structure is empty

angryTom
angryTomOriginal
2020-03-12 12:52:3322876browse

How to determine whether a golang structure is empty

How to determine whether the golang structure is empty

How to determine whether the golang structure is empty? To determine whether it has been initialized, the method is as follows:

You can use if objectA== (structname{}){ // your code } to determine.

The sample code is as follows:

package main

import (
    "fmt"
    "reflect"
)

type A struct{
    name string
    age int
}

func (a A) IsEmpty() bool {
    return reflect.DeepEqual(a, A{})
}

func main() {

    var a A

    if a == (A{}) {  // 括号不能去
        fmt.Println("a == A{} empty")
    }

    if a.IsEmpty() {
        fmt.Println("reflect deep is empty")
    }
}

For more programming tutorials, please pay attention to the PHP Chinese website!

The above is the detailed content of How to determine whether a golang structure is empty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn