5
gitpush
4y

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

Comments
  • 0
    This is the result of fmt & error I am receiving
  • 1
    Does go support implicit ref -> boolean conversion?
  • 0
    @SortOfTested what do you suggest?
  • 1
    @gitpush
    Break the short if assignment into its own statement and interrogate the variable.
  • 0
    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.
  • 0
    @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
  • 0
    @SortOfTested I tried your suggestion same result unfortunately :\
  • 0
    @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
  • 1
    @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.
  • 0
    @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
  • 2
  • 0
    @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
  • 1
    @gitpush
    It's something of a convention to use short, preferably one letter, names for local variables.
  • 0
    @metamourge OK noted, thank you for your suggestions much appreciated
  • 1
    @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
  • 0
    @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
Add Comment