使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value

使用gorm写了个小的demo, 插入和查询单个数据库都正确显示,在查询all数据时报错了。

错误信息

重点信息:using unaddressable value

reflect: reflect.flag.mustBeAssignable using unaddressable value
C:/F/yqgopath/pkg/mod/github.com/jinzhu/[email protected]/scope.go:865 (0xb197a6)
	(*Scope).callCallbacks.func1: panic(err)
C:/F/Go/src/runtime/panic.go:679 (0x430b8f)
	gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
C:/F/Go/src/reflect/value.go:247 (0x48f54e)
	flag.mustBeAssignableSlow: panic("reflect: " + methodName() + " using unaddressable value")
C:/F/Go/src/reflect/value.go:234 (0x4957f1)
	flag.mustBeAssignable: f.mustBeAssignableSlow()
C:/F/Go/src/reflect/value.go:1531 (0x4957d8)
	Value.Set: v.mustBeAssignable()
C:/F/yqgopath/pkg/mod/github.com/jinzhu/[email protected]/callback_query.go:48 (0xad949d)
	queryCallback: results.Set(reflect.MakeSlice(results.Type(), 0, 0))
C:/F/yqgopath/pkg/mod/github.com/jinzhu/[email protected]/scope.go:869 (0xb03218)
	(*Scope).callCallbacks: (*f)(scope)
C:/F/yqgopath/pkg/mod/github.com/jinzhu/[email protected]/main.go:354 (0xaf0e6e)
	(*DB).Find: return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
C:/F/GoLandProjects/mygotutorials/ginconfigdemo/pkg/controller/user_controller.go:57 (0xb40d30)
	(*UserController).GetAllUsers: result := c.db.Find(users)
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0xa9bef1)
	(*Context).Next: c.handlers[c.index](c)
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 (0xab03ba)
	RecoveryWithWriter.func1: c.Next()
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0xa9bef1)
	(*Context).Next: c.handlers[c.index](c)
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0xaaf4c7)
	LoggerWithConfig.func1: c.Next()
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0xa9bef1)
	(*Context).Next: c.handlers[c.index](c)
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:409 (0xaa6653)
	(*Engine).handleHTTPRequest: c.Next()
C:/F/yqgopath/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:367 (0xaa5d44)
	(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/F/Go/src/net/http/server.go:2802 (0x76e1fa)
	serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
C:/F/Go/src/net/http/server.go:1890 (0x7699cb)
	(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
C:/F/Go/src/runtime/asm_amd64.s:1357 (0x45c1c0)
	goexit: BYTE	$0x90	// NOP
	````

仔细对比发现就是查询传入的不是值

```go
	users := make([]model.User, 0)
	//这里有问题,一直无法查询全部内容,因为没有使用&users, 而是users
	result := c.db.Find(&users)
	if result.Error != nil {
		klog.Infof("failed to find all, err:%s", result.Error)
	}

错误信息截图
使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value_第1张图片

你可能感兴趣的:(go,golang,开发语言,后端)