Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
golang中积垢的一般处理措施_Go_Go Gorm - Fatal编程技术网

golang中积垢的一般处理措施

golang中积垢的一般处理措施,go,go-gorm,Go,Go Gorm,我正在尝试创建一个视图来处理我的gorm模型上的所有基本CRUD操作。 我们的目标是将模型传递给视图,让所有的奇迹发生 我找到了关于使用反射的话题,所以我找到了,但也读到了这不是“golang way” 我遇到的第一个问题是gorm总是使用“值”表。因此,临时解决方案是强制使用CommonView package controllers import ( "encoding/json" "fmt" "github.com/jinzhu/gorm" "net/ht

我正在尝试创建一个视图来处理我的gorm模型上的所有基本CRUD操作。 我们的目标是将模型传递给视图,让所有的奇迹发生

我找到了关于使用反射的话题,所以我找到了,但也读到了这不是“golang way”

我遇到的第一个问题是gorm总是使用“值”表。因此,临时解决方案是强制使用
CommonView

package controllers

import (
    "encoding/json"
    "fmt"
    "github.com/jinzhu/gorm"
    "net/http"
    "reflect"
)

type CommonView struct {
    db        *gorm.DB
    modelType reflect.Type
    model     interface{}
    tableName string
}

func NewCommonView(db *gorm.DB, model interface{}, tableName string) *CommonView {
    return &CommonView{
        db:        db,
        modelType: reflect.TypeOf(model),
        model:     model,
        tableName: tableName,
    }
}

func (cv *CommonView) HandleList(w http.ResponseWriter, r *http.Request) {
    modelSliceReflect := reflect.SliceOf(cv.modelType)
    models := reflect.MakeSlice(modelSliceReflect, 0, 10)

    fmt.Println(modelSliceReflect)
    fmt.Println(models)

    //modelsDirect := reflect.MakeSlice(reflect.TypeOf(cv.model), 0, 0)
    cv.db.Table("users").Find(&models)

    fmt.Println("From db: ")
    fmt.Println(models)

    modelType := reflect.TypeOf(modelSliceReflect)
    fmt.Println("Type name: " + modelType.String())


    modelsJson, _ := json.Marshal(models)

    fmt.Fprint(w, string(modelsJson))
}
型号: 包装模型

import "golang.org/x/crypto/bcrypt"

type User struct {
    Id        string `json:"id" gorm:"type:uuid;primary_key;default:uuid_generate_v4()"`
    FirstName string `json:"firstName"`
    LastName  string `json:"lastName"`
    Email     string `json:"email" gorm:"unique;not null"`
    Password  string `json:"-"`
}

func (User) TableName() string {
    return "user"
}
Gorm在DB中查找行(从Gorm日志知道这一点)。但是json不会转储它们——我猜它的类型是错误的,无法处理。 你知道怎么处理这个问题吗


如果您还有其他解决CRUD视图问题的方法,我也将非常感激。

问题源于json包处理的
reflect.Value
与预期不符。您可以在此处找到类似的讨论:

正如您在下面的代码片段中所看到的,
reflect.MakeSlice
返回的是类型
值,而不是切片

slice_empty_reflect_make := reflect.MakeSlice(
                                    reflect.SliceOf(
                                            reflect.TypeOf(5)),
                                    10, 10)

fmt.Printf("Type of reflect.MakeSlice(): %s\n",
           reflect.TypeOf(slice_empty_reflect_make).Name())
这将产生:

Type of reflect.MakeSlice(): Value
在json封送器中输入
值时,它将返回一个对象,而不是数组:

Json: {}
Error: <nil>

这是一个伪装成

的副本,您不应该忽略Go中的错误:
modelsJson,:=json.Marshal(modelsicereflect)
-这里的错误可能会显示封送失败的原因。忘记这一点,但没有错误。您用gorm填充
模型
,但封送
modelsicereflect
。这看起来不一致。@thst哦。。。可能是经过奇怪的测试后离开的。无论如何,这并没有解决任何问题,但谢谢你的指点。(代码编辑)你会试图抓住马歇尔的错误并将其打印出来吗?不知道为什么您如此确信,在封送处理上没有错误。。。
jsonBytes, err := json.Marshal(slice_empty_reflect_make.Interface())