Problem overview
The starter code has a common map, map[string]bool, listing well-known weak passwords: password, 123456, qwerty and letmein. It also has the password to check. The program prints the password, whether it is on the list, and a verdict. For qwerty:
Password: qwerty
Common: True
Weak: reject
For Tr0ub4dor, which is not on the list, the output is Password: Tr0ub4dor, Common: False and Strong: accept. The helper boolText(b) is provided and returns "True" or "False".
Approach: a map used as a set
Checking membership in a list means comparing against each entry until one matches. A map finds a key directly, so the cost of the check does not grow with the number of entries. Go has no built-in set type, so the usual way to make a set is a map[string]bool whose present keys have the value true.
The lookup common[password] then answers the question in one expression. If the password is a key, the result is its value, true. If it is not a key, Go gives the zero value of the value type, and for bool that is false. So no separate "does the key exist" test is needed.
The check is exact. Qwerty with a capital Q is a different key from qwerty, so it is not found.
Step by step
- Print the password line.
"Password: " + passwordjoins two strings, so no conversion is needed.fmt.Printlnprints it. - Look the password up.
isCommon := common[password]stores the lookup result, abool. - Print the
Common:line withboolText(isCommon)to get the capitalized text. Printing the boolean directly would give lowercasetrue. - Write the verdict.
if isCommonis enough as the condition, sinceisCommonis already a boolean. Comparing it totrueadds nothing. The body printsWeak: reject. - Add the
else. It runs when the password is not on the list, and printsStrong: accept. The closing brace of theifand theelseshare a line.
The solution in Go
common := map[string]bool{"password": true, "123456": true, "qwerty": true, "letmein": true}
password := "qwerty"
fmt.Println("Password: " + password)
isCommon := common[password]
fmt.Println("Common: " + boolText(isCommon))
if isCommon {
fmt.Println("Weak: reject")
} else {
fmt.Println("Strong: accept")
}
Go details worth knowing here:
- Missing keys give the zero value. Reading
common["hello"]does not fail and does not add a key. It givesfalse. - The two-value lookup.
v, ok := common[password]also reports whether the key exists. For a set oftruevalues the plain form is enough, because absence andfalsemean the same. The two-value form matters whenfalsecould be a stored value. - Booleans need a helper to print as
True.fmt.Println(isCommon)printstrue. The lesson's output wants a capital letter, which is whyboolTextexists. - Short variable declarations.
isCommon := ...declares and assigns in one step, and the typeboolis inferred from the map's value type. - Empty struct sets. Some Go programs use
map[string]struct{}as a set, because the value takes no space. This lesson'smap[string]boolis easier to read and is what the starter uses.
Edge cases and common mistakes
- Case differences.
QwertygivesCommon: FalseandStrong: accept, because only the exact lowercase text is a key. Converting the password withstrings.ToLowerbefore the lookup would catch these. - An empty password.
""is not a key, so the program printsCommon: FalseandStrong: accept. This version has no length rule. - Calling "accept" strong. The only rule here is the list, so a password that is not on the list is accepted whatever it looks like. The output text follows the lesson's wording.
- Comparing with
== true.if isCommon == truecompiles but is redundant. - Spelling in the output. The lines must be exactly
Weak: rejectandStrong: accept.
Testing
qwertyprintsCommon: TrueandWeak: reject.Tr0ub4dorprintsCommon: FalseandStrong: accept.QwertyprintsCommon: FalseandStrong: accept.- An empty string prints
Password:,Common: FalseandStrong: accept.
