Conversation
zayanh
commented
Oct 30, 2016
| // homeT will store the html template to be used in this website | ||
| var homeT *template.Template | ||
|
|
||
| var mu = &sync.Mutex{} |
Owner
Author
There was a problem hiding this comment.
Move the mutex declaration near the data that it protects:
var d data
var dataMtx = &sync.Mutex{}
zayanh
commented
Oct 30, 2016
| // Initialize map of usernames | ||
| mu.Lock() | ||
| d.UserNames = make(map[string]bool) | ||
| mu.Unlock() |
zayanh
commented
Oct 30, 2016
| // Open file to read and write. Create if it doesn't exist. Append if it does | ||
| f, err := os.OpenFile(filename, os.O_CREATE | os.O_RDWR | os.O_APPEND, 0666) | ||
| if err != nil { | ||
| //handle error |
zayanh
commented
Oct 30, 2016
| if err != nil { | ||
| //handle error | ||
| } | ||
| userFile = f // use global variable -- is this approach okay? |
spectreman
reviewed
Oct 30, 2016
| for scanner.Scan() { | ||
| mu.Lock() | ||
| d.UserNames[scanner.Text()] = true | ||
| mu.Unlock() |
spectreman
reviewed
Oct 30, 2016
| } | ||
| // Err returns the first non-EOF error encoutnered by the scanner | ||
| if err := scanner.Err(); err != nil { | ||
| // handle error |
spectreman
reviewed
Oct 30, 2016
| // Check if the username already exists | ||
| mu.Lock() | ||
| if _,ok := d.UserNames[username]; ok { | ||
| defer mu.Unlock() // is this okay? |
Contributor
There was a problem hiding this comment.
defer is redundant. Just do
mu.Unlock()
spectreman
reviewed
Oct 30, 2016
| } | ||
|
|
||
| // Check if the username already exists | ||
| mu.Lock() |
Contributor
There was a problem hiding this comment.
You can do:
mu.Lock()
defer mu.Unlock()
spectreman
reviewed
Oct 30, 2016
| d.Message = "User " + username + " added successfully" | ||
| } | ||
| http.Redirect(w, r, "/home", http.StatusFound) | ||
| } |
spectreman
reviewed
Oct 30, 2016
| duplicate entries would take O(n) time, while searching a map only takes O(1) time. */ | ||
| UserNames map[string]bool | ||
| // Message will inform the user of the results from the previous attempt to add a new user | ||
| Message string |
Contributor
There was a problem hiding this comment.
Messages map[*http.Request]string
Then down below:
func Setup() {
...
Messages = make(map[*http.Request]string)
}
We can disucss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.