×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to check if structure is empty in Golang?

By IncludeHelp Last updated : October 05, 2024

What is Golang Empty Structure?

The size of an empty structure is zero in Golang. Here, empty structure means, there is no field in the structure.

Syntax

    Type structure_name struct {
    }

Example 1

In Go, structs without fields are considered "empty structs." In the code, a Person struct is defined with no fields. The main function compares an empty struct Person{} to the variable st, also initialized as an empty struct, resulting in equality and printing "It is an empty structure."

package main

import (
  "fmt"
)

type Person struct {

}

func main() {
  var st Person
  if (Person{} == st) {
      fmt.Println("It is an empty structure")
  } else {
    fmt.Println("It is not an empty structure")
  }
}
It is an empty structure

Example 2

In this Go code, the Person struct has an age field. The variable st is initialized with a default age value of 0. When compared to Person{20}, the condition evaluates as false since the age values differ, resulting in "It is not an empty structure" being printed.

package main

import (
  "fmt"
)

type Person struct {
  age int
}

func main() {
  var st Person
  if (Person{20} == st) {
      fmt.Println("It is an empty structure")
  } else {
    fmt.Println("It is not an empty structure")
  }
}
It is not an empty structure

Check if structure is empty when it has fields

If a structure has fields, then how to check whether structure has been initialised or not?

Please follow given below examples...

Syntax

    Type structure_name struct {
        variable_name type
    }

Example 1

In this code, the IsStructureEmpty method uses reflect.DeepEqual to compare a Person struct to its zero value Person{}. If all fields match the zero value (e.g., age is 0), it returns true. In main, this method checks if the struct is empty and prints accordingly.

package main

import (
  "fmt"
  "reflect"
)

type Person struct {
  age int
}

func (x Person) IsStructureEmpty() bool {
  return reflect.DeepEqual(x, Person{})
}

func main() {
  x := Person{}

  if x.IsStructureEmpty() {
    fmt.Println("Structure is empty")
  } else {
    fmt.Println("Structure is not empty")
  }
}

Output

Structure is empty

Example 2

In this code, the IsStructureEmpty method uses reflect.DeepEqual to compare the Person struct x to its zero value (Person{}). The age field of x is set to 20, making it non-empty. As a result, the method returns false, and the program prints "Structure is not empty."

package main

import (
  "fmt"
  "reflect"
)

type Person struct {
  age int
}

func (x Person) IsStructureEmpty() bool {
  return reflect.DeepEqual(x, Person{})
}

func main() {
  x := Person{}
  x.age = 20
  if x.IsStructureEmpty() {
    fmt.Println("Structure is empty")
  } else {
    fmt.Println("Structure is not empty")
  }
}

Output

Structure is not empty

Check if structure is empty Using switch statement

To check if a structure is empty using a switch statement in Go, you can compare it to its zero value.

Example 1

In this code, the IsStructureEmpty method uses reflect.DeepEqual to check if a Person struct is empty by comparing it to a zero-value Person{}. If the fields match, it returns true. In main, the method is called to determine if the struct is empty.

package main

import (
  "fmt"
)

type Person struct {
}

func main() {
  x := Person{}

  switch {
    case x == Person{}:
        fmt.Println("Structure is empty")
    default:
        fmt.Println("Structure is not empty")
  }
}

Output

Structure is empty

Example 2

In this code, a Person struct with an age field is initialized as empty (Person{}). The switch statement checks if x equals Person{1}. Since the age field is 0 in x, the condition fails, and the default case prints "Structure is not empty."

package main

import (
  "fmt"
)

type Person struct {
  age int
}

func main() {
  x := Person{}

  switch {
    case x == Person{1}:
        fmt.Println("Structure is empty")
    default:
        fmt.Println("Structure is not empty")
  }
}

Output

Structure is not empty


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.