Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@gitpush
Break the short if assignment into its own statement and interrogate the variable. -
First of all,
Learn to write proper go code.
That shit is ugly as all hell.
Second,
I'd say that result.Error!=nil is the culprit,
Because you are doing a nil pointer dereference, if result is nil. -
@metamourge Can you show how that code should be written?
and if I remove result.Error != nil, then if condition will be valid ==> panic when printing error -
@SortOfTested To be precise the error is said to be on this line: repo.db.Where("id = ?", ID).First(&user)
But printing user value gives a valid output so I'm not sure tbh on what's wrong -
@gitpush
Tediously break out and interrogate each step of your short circuit, both result and result.Error check as well. The should give you an intuition about the behavior you're seeing. -
@SortOfTested This code does not panic, it shows invalid value. I checked ORM source code that I'm using, when this is printed this means "user" address is zero = no address in memory.
While the code that is in my rant inits user to a valid value, I'm suspecting that the issue is user is being cleaned when function execution is over?
But if I understood it correctly, my function has signature of: *models.User, * = return a copy of the value that is being pointed at? Not 100% sure tho -
@metamourge Thank you but I was wondering why "u" instead of "user" for a var name? Is it a practice in golang or just preference? since user is more descriptive
-
@gitpush
It's something of a convention to use short, preferably one letter, names for local variables. -
@gitpush
Didn't feel the need to reply when a picture is worth a thousand words :p
That said, one of my "language learning hacks" is to spend a day going through whatever linter/stylechecker is commonly used and write example code from every rule. It gets you up to speed quick-like while also grokking conventions:
https://github.com/golang/lint -
@SortOfTested haha its ok don't worry about it, however I did make some progress, I have another function called: FindByEmail, that one works but FindByID doesn't. The ORM that I use has ID field defaults to int, I did write a hook for it to make the field as string and accept uuid. I'm guessing this is where I screwed up, because FindByID & FindByEmail have exact same code but one filters by email while the other filters by ID
Related Rants
I give up. I am having trouble understanding Go lang *, & stuff.
attached code does print what fmt.Println has, but the function calling: FindByID is throwing invalid memory address or nil pointer when it attempts to use the result of the code.
Can someone please explain where I'm wrong?
Error log blames the line of "if" condition even though fmt does print its output to console
question
golang