Problem overview
The password must satisfy two rules: at least 8 characters, and at least one digit. The program prints the length, whether a digit was found, and a verdict, Strong or Weak. For Secret99:
Length: 8
Has digit: True
Strong
With abcdefgh the length is 8 but there is no digit, so the output is Length: 8, Has digit: False and Weak. The lesson's file already contains a helper, boolText(b), that returns the text "True" or "False" for a boolean, since Go prints booleans as lowercase true and false.
Approach: a flag set during one pass
The length is a single call. The digit rule needs a look at every character. The usual tool is a boolean flag: start it at false, loop over the password, and set it to true when a digit appears. Nothing in the loop sets it back to false, so one digit anywhere is enough, and the flag's final value answers "is there a digit?".
A character is a digit when it lies between '0' and '9'. Characters are ordered by their numeric codes, and the ten digit characters have consecutive codes, so two comparisons identify them.
The standard library has shortcuts for the same check. strings.ContainsAny(password, "0123456789") returns true when the password has any of the listed characters, and unicode.IsDigit(char) tests one rune. The lesson writes the loop by hand so that you see how a flag works. The hand-written range test is also stricter than unicode.IsDigit: the Arabic-Indic digit '٣' counts as a digit for unicode.IsDigit but falls outside '0' to '9'.
Keeping length and the flag separate until the verdict also keeps the output easy to read: each printed line reports one fact, and the final Strong or Weak follows from those facts. When a password comes out Weak, the two lines above it show which rule it missed: a short length, a missing digit, or both. A version that printed only the verdict would hide that.
Step by step
- Store the length in
lengthwithlen(password). It is used for the printed line and for the 8-character rule. - Print the first line. The label
"Length: "is joined tostrconv.Itoa(length), because Go does not join anintto a string with+. - Create the flag.
hasDigit := falseinfers the typeboolfrom the value. - Loop over the characters.
for _, char := range passwordgives two values per round: the position and the character. The blank identifier_discards the position, which isn't needed. Go would reject an unused variable. - Test for a digit inside the loop.
char >= '0' && char <= '9'must hold for both parts. When it does, sethasDigit = true. - Print the flag after the loop, converted with
boolText. This line is outside the braces so it runs once. - Write the verdict.
length >= 8 && hasDigitjoins the two rules with&&, which is true only when both sides are true. Theifbranch printsStrong. - Add the
elseon the closing-brace line. It printsWeakfor every password that fails either rule or both.
The solution in Go
password := "Secret99"
length := len(password)
fmt.Println("Length: " + strconv.Itoa(length))
hasDigit := false
for _, char := range password {
if char >= '0' && char <= '9' {
hasDigit = true
}
}
fmt.Println("Has digit: " + boolText(hasDigit))
if length >= 8 && hasDigit {
fmt.Println("Strong")
} else {
fmt.Println("Weak")
}
Go details worth knowing here:
rangeover a string yields runes. Eachcharis arune, which is an alias forint32: the Unicode code point of the character. A literal like'0'is a rune constant, so it compares withchardirectly. Compare this with indexing:password[i]gives abyte, which is one piece of the UTF-8 encoding.- The two counts can differ.
len(password)is in bytes, whilerangewalks characters. For ASCII passwords they agree. For a password withé,rangesees one rune andlencounts two bytes. - Single and double quotes differ.
'0'is one character (a rune)."0"is a string. Comparing a rune to a string does not compile. &&short-circuits. If the left side is false, the right side is not evaluated. Here neither side has side effects, so it only saves a comparison.
Edge cases and common mistakes
- Long but no digit.
abcdefghprintsHas digit: FalseandWeak. - A digit but too short.
abc1printsLength: 4,Has digit: TrueandWeak. - Only digits.
12345678isStrongunder these two rules. Nothing here asks for letters. - Resetting the flag in the loop. An
elsethat setshasDigit = falsefor non-digits would undo an earlier match. WithSecret99the last character is a digit, but with1abcdefgit would end upfalse. - Printing the boolean directly. Joining
hasDigitto a string fails to compile. Andfmt.Println(hasDigit)printstruein lowercase, while the expected line saysTrue, so the helper is needed. - Using
||in the digit test.char >= '0' || char <= '9'is true for every character, because every code is either at least that of'0'or at most that of'9'.
Testing
Secret99printsLength: 8,Has digit: TrueandStrong.abcdefghprintsLength: 8,Has digit: FalseandWeak.abc1printsLength: 4,Has digit: TrueandWeak.12345678printsLength: 8,Has digit: TrueandStrong.
