docs/core/gdb-result-empty-array #282
Replies: 1 comment
-
// User description
type User struct {
Name string
}
// TestFuncName description
func TestFuncName(t *testing.T) {
var a []*User
b := InitSlice(1, a)
t.Log(b)
}
// Resp description
type Resp struct {
Code int
Data any
}
// InitSlice description
// 如果data是切片且为nil,则初始化为空切片后再序列化
func InitSlice(code int, data any) string {
if data != nil {
v := reflect.ValueOf(data)
// 如果是切片类型且为nil,初始化为空切片
if v.Kind() == reflect.Slice && v.IsNil() {
data = reflect.MakeSlice(v.Type(), 0, 0).Interface()
}
}
resp := Resp{
Code: code,
Data: data,
}
jsonb, _ := json.Marshal(resp)
return string(jsonb)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
docs/core/gdb-result-empty-array
在GoFrame框架中处理ORM查询结果,当未查询到数据时,通过初始化空数组避免返回null值,从而增强与前端的友好交互。这种改进可以在数据需要展示于网页时,确保返回格式的预测性与稳定性。
https://goframe.org/docs/core/gdb-result-empty-array
Beta Was this translation helpful? Give feedback.
All reactions