Passing pointer as interface type
Help me. I'm stuck at this, I make a checkRecordExistence to check for the existence of a record.This function take a model as an interface type, i pass the model down to the First function which also take model as an interface type but it doesnt work
func checkRecordExistence(model interface, query string, field ...interface) bool
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err)
return false
return true
// GetPlaylists of a specific user
func GetPlaylists(username string) (playlists Playlist)
playlists = Playlist
user := User
if exist := checkRecordExistence(user, "username = ?", username); !exist
return
db.GetDB().Preload("Songs.Artists").Preload("Songs").Model(&user).Related(&playlists, "UserRefer")
return
When i call the GetPlaylists function it crashed
eflect.Value.Addr of unaddressable value
/usr/lib/go/src/runtime/panic.go:513 (0x42c4d8)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/lib/go/src/reflect/value.go:245 (0x4ae0c5)
Value.Addr: panic("reflect.Value.Addr of unaddressable value")
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/callback_query.go:74 (0x54f1b4)
queryCallback: scope.scan(rows, columns, scope.New(elem.Addr().Interface()).Fields())
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/scope.go:857 (0x5767a4)
(*Scope).callCallbacks: (*f)(scope)
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/main.go:289 (0x56528e)
(*DB).First: inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35 (0x676c06)
checkRecordExistence: if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) {
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:45 (0x676dfc)
GetPlaylists: if exist := checkRecordExistence(user, "username = ?", username); !exist {
I think the problem is here: .Where(query, field...).First(model) but i can't find any solution, thanks if anyone can help me.
go
add a comment |
Help me. I'm stuck at this, I make a checkRecordExistence to check for the existence of a record.This function take a model as an interface type, i pass the model down to the First function which also take model as an interface type but it doesnt work
func checkRecordExistence(model interface, query string, field ...interface) bool
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err)
return false
return true
// GetPlaylists of a specific user
func GetPlaylists(username string) (playlists Playlist)
playlists = Playlist
user := User
if exist := checkRecordExistence(user, "username = ?", username); !exist
return
db.GetDB().Preload("Songs.Artists").Preload("Songs").Model(&user).Related(&playlists, "UserRefer")
return
When i call the GetPlaylists function it crashed
eflect.Value.Addr of unaddressable value
/usr/lib/go/src/runtime/panic.go:513 (0x42c4d8)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/lib/go/src/reflect/value.go:245 (0x4ae0c5)
Value.Addr: panic("reflect.Value.Addr of unaddressable value")
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/callback_query.go:74 (0x54f1b4)
queryCallback: scope.scan(rows, columns, scope.New(elem.Addr().Interface()).Fields())
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/scope.go:857 (0x5767a4)
(*Scope).callCallbacks: (*f)(scope)
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/main.go:289 (0x56528e)
(*DB).First: inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35 (0x676c06)
checkRecordExistence: if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) {
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:45 (0x676dfc)
GetPlaylists: if exist := checkRecordExistence(user, "username = ?", username); !exist {
I think the problem is here: .Where(query, field...).First(model) but i can't find any solution, thanks if anyone can help me.
go
can you post code around this line/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35
?
– Dmitry Harnitski
Nov 14 '18 at 0:31
this line:if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) return false
– Anh Thi
Nov 14 '18 at 0:33
did you tryuser := &User
?
– Dmitry Harnitski
Nov 14 '18 at 0:38
I'm so dumb for not realizing this thank you @DmitryHarnitski
– Anh Thi
Nov 14 '18 at 0:43
Funny, your question topic contains an answer :)
– Dmitry Harnitski
Nov 14 '18 at 0:45
add a comment |
Help me. I'm stuck at this, I make a checkRecordExistence to check for the existence of a record.This function take a model as an interface type, i pass the model down to the First function which also take model as an interface type but it doesnt work
func checkRecordExistence(model interface, query string, field ...interface) bool
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err)
return false
return true
// GetPlaylists of a specific user
func GetPlaylists(username string) (playlists Playlist)
playlists = Playlist
user := User
if exist := checkRecordExistence(user, "username = ?", username); !exist
return
db.GetDB().Preload("Songs.Artists").Preload("Songs").Model(&user).Related(&playlists, "UserRefer")
return
When i call the GetPlaylists function it crashed
eflect.Value.Addr of unaddressable value
/usr/lib/go/src/runtime/panic.go:513 (0x42c4d8)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/lib/go/src/reflect/value.go:245 (0x4ae0c5)
Value.Addr: panic("reflect.Value.Addr of unaddressable value")
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/callback_query.go:74 (0x54f1b4)
queryCallback: scope.scan(rows, columns, scope.New(elem.Addr().Interface()).Fields())
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/scope.go:857 (0x5767a4)
(*Scope).callCallbacks: (*f)(scope)
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/main.go:289 (0x56528e)
(*DB).First: inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35 (0x676c06)
checkRecordExistence: if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) {
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:45 (0x676dfc)
GetPlaylists: if exist := checkRecordExistence(user, "username = ?", username); !exist {
I think the problem is here: .Where(query, field...).First(model) but i can't find any solution, thanks if anyone can help me.
go
Help me. I'm stuck at this, I make a checkRecordExistence to check for the existence of a record.This function take a model as an interface type, i pass the model down to the First function which also take model as an interface type but it doesnt work
func checkRecordExistence(model interface, query string, field ...interface) bool
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err)
return false
return true
// GetPlaylists of a specific user
func GetPlaylists(username string) (playlists Playlist)
playlists = Playlist
user := User
if exist := checkRecordExistence(user, "username = ?", username); !exist
return
db.GetDB().Preload("Songs.Artists").Preload("Songs").Model(&user).Related(&playlists, "UserRefer")
return
When i call the GetPlaylists function it crashed
eflect.Value.Addr of unaddressable value
/usr/lib/go/src/runtime/panic.go:513 (0x42c4d8)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/lib/go/src/reflect/value.go:245 (0x4ae0c5)
Value.Addr: panic("reflect.Value.Addr of unaddressable value")
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/callback_query.go:74 (0x54f1b4)
queryCallback: scope.scan(rows, columns, scope.New(elem.Addr().Interface()).Fields())
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/scope.go:857 (0x5767a4)
(*Scope).callCallbacks: (*f)(scope)
/home/anhthi/go/src/github.com/anhthii/go-echo/vendor/github.com/jinzhu/gorm/main.go:289 (0x56528e)
(*DB).First: inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35 (0x676c06)
checkRecordExistence: if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) {
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:45 (0x676dfc)
GetPlaylists: if exist := checkRecordExistence(user, "username = ?", username); !exist {
I think the problem is here: .Where(query, field...).First(model) but i can't find any solution, thanks if anyone can help me.
go
go
asked Nov 14 '18 at 0:18
Anh ThiAnh Thi
93
93
can you post code around this line/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35
?
– Dmitry Harnitski
Nov 14 '18 at 0:31
this line:if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) return false
– Anh Thi
Nov 14 '18 at 0:33
did you tryuser := &User
?
– Dmitry Harnitski
Nov 14 '18 at 0:38
I'm so dumb for not realizing this thank you @DmitryHarnitski
– Anh Thi
Nov 14 '18 at 0:43
Funny, your question topic contains an answer :)
– Dmitry Harnitski
Nov 14 '18 at 0:45
add a comment |
can you post code around this line/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35
?
– Dmitry Harnitski
Nov 14 '18 at 0:31
this line:if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) return false
– Anh Thi
Nov 14 '18 at 0:33
did you tryuser := &User
?
– Dmitry Harnitski
Nov 14 '18 at 0:38
I'm so dumb for not realizing this thank you @DmitryHarnitski
– Anh Thi
Nov 14 '18 at 0:43
Funny, your question topic contains an answer :)
– Dmitry Harnitski
Nov 14 '18 at 0:45
can you post code around this line
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35
?– Dmitry Harnitski
Nov 14 '18 at 0:31
can you post code around this line
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35
?– Dmitry Harnitski
Nov 14 '18 at 0:31
this line:
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) return false
– Anh Thi
Nov 14 '18 at 0:33
this line:
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) return false
– Anh Thi
Nov 14 '18 at 0:33
did you try
user := &User
?– Dmitry Harnitski
Nov 14 '18 at 0:38
did you try
user := &User
?– Dmitry Harnitski
Nov 14 '18 at 0:38
I'm so dumb for not realizing this thank you @DmitryHarnitski
– Anh Thi
Nov 14 '18 at 0:43
I'm so dumb for not realizing this thank you @DmitryHarnitski
– Anh Thi
Nov 14 '18 at 0:43
Funny, your question topic contains an answer :)
– Dmitry Harnitski
Nov 14 '18 at 0:45
Funny, your question topic contains an answer :)
– Dmitry Harnitski
Nov 14 '18 at 0:45
add a comment |
1 Answer
1
active
oldest
votes
According to https://github.com/jinzhu/gorm/issues/394, parameter model
should be a pointer. I think the code below would work.
if exist := checkRecordExistence(&user, "username = ?", username); !exist
return
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291368%2fpassing-pointer-as-interface-type%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to https://github.com/jinzhu/gorm/issues/394, parameter model
should be a pointer. I think the code below would work.
if exist := checkRecordExistence(&user, "username = ?", username); !exist
return
add a comment |
According to https://github.com/jinzhu/gorm/issues/394, parameter model
should be a pointer. I think the code below would work.
if exist := checkRecordExistence(&user, "username = ?", username); !exist
return
add a comment |
According to https://github.com/jinzhu/gorm/issues/394, parameter model
should be a pointer. I think the code below would work.
if exist := checkRecordExistence(&user, "username = ?", username); !exist
return
According to https://github.com/jinzhu/gorm/issues/394, parameter model
should be a pointer. I think the code below would work.
if exist := checkRecordExistence(&user, "username = ?", username); !exist
return
answered Nov 14 '18 at 0:40
Kitt HsuKitt Hsu
11714
11714
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291368%2fpassing-pointer-as-interface-type%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you post code around this line
/home/anhthi/go/src/github.com/anhthii/go-echo/db/models/playlist.go:35
?– Dmitry Harnitski
Nov 14 '18 at 0:31
this line:
if err := db.GetDB().Where(query, field...).First(model).Error; gorm.IsRecordNotFoundError(err) return false
– Anh Thi
Nov 14 '18 at 0:33
did you try
user := &User
?– Dmitry Harnitski
Nov 14 '18 at 0:38
I'm so dumb for not realizing this thank you @DmitryHarnitski
– Anh Thi
Nov 14 '18 at 0:43
Funny, your question topic contains an answer :)
– Dmitry Harnitski
Nov 14 '18 at 0:45