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

How to determine whether a golang structure is empty

angryTom
Release: 2020-03-12 12:52:33
Original
22966 people have browsed it

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")
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template