Awesome article. I wish you would come for the Golang meetup tomorrow.
Anyway, there is just one thing I would want to comment about on your article, especially for newcomers who might end up copying and pasting.
loggedInUser is a global variable that would be accessed from the handlers. So, the way Go router works is that it creates a goroutine to handle each request, and with all the goroutines accessing that struct, it would mean 2 things:
Only one user can be logged in at a time. If another user logs in, it would overwrite that variable in memory.
In an application which would have multiple requests being handled at a time, you are going to have a data race (multiple goroutines accessing the same resource in memory). A solution could be to use mutex locks, but that is also not a good solution depending on who you ask. It's a better idea to avoid global variables in the first place, and architect your application such that you never need to use locks.
If you must use a global+mutex lock, at least store the data as a map of say; usernames, to the user struct so multiple users can be logged in at once. I noticed a few other things, but they are minor.
Great article, still.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Awesome article. I wish you would come for the Golang meetup tomorrow.
Anyway, there is just one thing I would want to comment about on your article, especially for newcomers who might end up copying and pasting.
loggedInUser
is a global variable that would be accessed from the handlers. So, the way Go router works is that it creates a goroutine to handle each request, and with all the goroutines accessing that struct, it would mean 2 things:If you must use a global+mutex lock, at least store the data as a map of say; usernames, to the user struct so multiple users can be logged in at once. I noticed a few other things, but they are minor.
Great article, still.