Skip to main content
Version: 2.8.x(Latest)

Pain Point Description

As described in the previous chapters, if a given array is uninitialized (with a value of nil), the ORM will not automatically initialize that array when no data is queried based on the given conditions. Thus, if this uninitialized array result is encoded via JSON, it will be converted to a null value.

package main

import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"

"fmt"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)

func main() {
type User struct {
Id uint64 // Primary Key
Passport string // Account
Password string // Password
NickName string // Nickname
CreatedAt *gtime.Time // Creation Time
UpdatedAt *gtime.Time // Update Time
}
type Response struct {
Users []User
}
var res = &Response{}
err := g.Model("user").WhereGT("id", 10).Scan(&res.Users)
fmt.Println(err)
fmt.Println(gjson.MustEncodeString(res))
}

After execution, the terminal displays the result as:

<nil>
{"Users":null}

In most scenarios, the data queried by the ORM needs to be rendered on a browser page, which means the returned data needs to be processed by frontend JS. To make it more friendly for frontend JS to handle backend return data, when no data is queried on the backend, it is expected to return an empty array structure instead of a null attribute value.

Improvement Plan

In this scenario, you can provide an initialized empty array to the Scan method of the ORM. When the ORM doesn't query any data, the array attribute remains an empty array, rather than nil, and the returned JSON encoding will not be a null value.

package main

import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"

"fmt"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)

func main() {
type User struct {
Id uint64 // Primary Key
Passport string // Account
Password string // Password
NickName string // Nickname
CreatedAt *gtime.Time // Creation Time
UpdatedAt *gtime.Time // Update Time
}
type Response struct {
Users []User
}
var res = &Response{
Users: make([]User, 0),
}
err := g.Model("user").WhereGT("id", 10).Scan(&res.Users)
fmt.Println(err)
fmt.Println(gjson.MustEncodeString(res))
}

After execution, the terminal displays the result as:

<nil>
{"Users":[]}