Using GoFrame ORM for checking if the returned result is empty is very simple. In most scenarios, you can directly check if the returned data is nil or has a length of 0, or use the IsEmpty/IsNil methods.

I. Data Sets (Multiple)

  1. r, err := g.Model("order").Where("status", 1).All()
  2. if err != nil {
  3. return err
  4. }
  5. if len(r) == 0 {
  6. // Result is empty
  7. }

You can also use the IsEmpty method:

  1. r, err := g.Model("order").Where("status", 1).All()
  2. if err != nil {
  3. return err
  4. }
  5. if r.IsEmpty() {
  6. // Result is empty
  7. }

II. Data Records (Single)

  1. r, err := g.Model("order").Where("status", 1).One()
  2. if err != nil {
  3. return err
  4. }
  5. if len(r) == 0 {
  6. // Result is empty
  7. }

You can also use the IsEmpty method:

  1. r, err := g.Model("order").Where("status", 1).One()
  2. if err != nil {
  3. return err
  4. }
  5. if r.IsEmpty() {
  6. // Result is empty
  7. }

III. Data Field Value

The return is a “generic” variable, so you can only use IsEmpty to determine if it is empty.

  1. r, err := g.Model("order").Where("status", 1).Value()
  2. if err != nil {
  3. return err
  4. }
  5. if r.IsEmpty() {
  6. // Result is empty
  7. }

IV. Field Value Array

The returned field value array is of type []gdb.Value, so you can directly check if the length is 0.

  1. // Array/FindArray
  2. r, err := g.Model("order").Fields("id").Where("status", 1).Array()
  3. if err != nil {
  4. return err
  5. }
  6. if len(r) == 0 {
  7. // Result is empty
  8. }

V. Struct Object (🔥Note🔥)

For Struct conversion object, there is a slight difference. Let’s look at an example.

When the passed object is a null pointer, if data is found in the query, this object will be automatically created internally. If no data is found, the null pointer remains a null pointer and no internal handling is done.

  1. var user *User
  2. err := g.Model("order").Where("status", 1).Scan(&user)
  3. if err != nil {
  4. return err
  5. }
  6. if user == nil {
  7. // Result is empty
  8. }

When the passed object is already an initialized object, if data is found, it will be assigned internally to this object. If no data is found, you cannot use nil to check for an empty result. Therefore, ORM will return an sql.ErrNoRows error, indicating to the developer that no data has been found and no assignments were made, allowing further empty result checks.

  1. var user = new(User)
  2. err := g.Model("order").Where("status", 1).Scan(&user)
  3. if err != nil && err != sql.ErrNoRows {
  4. return err
  5. }
  6. if err == sql.ErrNoRows {
  7. // Result is empty
  8. }

ORM Result - Empty Check - 图1tip

Therefore, we recommend that developers do not pass an already initialized object to ORM, but rather pass a pointer to a pointer of the object (**struct type). Internally, ORM will smartly perform automatic initialization based on the query result.

VI. Struct Array

When the passed object array is initially an empty array (length 0), if data is found, it will be automatically assigned internally to the array. If no data is found, the empty array remains an empty array, and no internal handling is done.

  1. var users []*User
  2. err := g.Model("order").Where("status", 1).Scan(&users)
  3. if err != nil {
  4. return err
  5. }
  6. if len(users) == 0 {
  7. // Result is empty
  8. }

When the passed object array is not an empty array, if data is found, it will be overridden internally onto the array from index 0. If no data is found, you cannot judge the empty result using length 0. Therefore, ORM will return an sql.ErrNoRows error, indicating that no data has been found and no assignments were made, allowing further empty result checks.

  1. var users = make([]*User, 100)
  2. err := g.Model("order").Where("status", 1).Scan(&users)
  3. if err != nil {
  4. return err
  5. }
  6. if err == sql.ErrNoRows {
  7. // Result is empty
  8. }

ORM Result - Empty Check - 图2warning

Due to Golang reflection features employed in struct conversion, there is a certain performance overhead. If you are dealing with conversions of large numbers of query result data into struct array objects and need to improve conversion performance, please refer to the custom implementation of the corresponding struct UnmarshalValue method: Type Conversion - Interface