Start learning free

LESSON 196 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 in Go: Length and Digit Check

Practice Read the full explanation

Check a password with two rules: it must have at least 8 characters and contain at least one digit.

You start with:

password := "Secret99"

Build:

Length: 8
Has digit: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Loop through each character to set a boolean flag hasDigit, which starts as false and becomes true when a digit is seen.
  • Print "Has digit: " followed by the flag. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If the length is at least 8 and hasDigit is true, print Strong; otherwise print Weak.
  • Use strconv.Itoa(n) to turn a number into text before joining it to a string.
  • The helper boolText(b) returns "True" or "False" for a boolean.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak
STEP 1 OF 8
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V2 in Go, explained

    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

    1. Store the length in length with len(password). It is used for the printed line and for the 8-character rule.
    2. Print the first line. The label "Length: " is joined to strconv.Itoa(length), because Go does not join an int to a string with +.
    3. Create the flag. hasDigit := false infers the type bool from the value.
    4. Loop over the characters. for _, char := range password gives 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.
    5. Test for a digit inside the loop. char >= '0' && char <= '9' must hold for both parts. When it does, set hasDigit = true.
    6. Print the flag after the loop, converted with boolText. This line is outside the braces so it runs once.
    7. Write the verdict. length >= 8 && hasDigit joins the two rules with &&, which is true only when both sides are true. The if branch prints Strong.
    8. Add the else on the closing-brace line. It prints Weak for 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:

    • range over a string yields runes. Each char is a rune, which is an alias for int32: the Unicode code point of the character. A literal like '0' is a rune constant, so it compares with char directly. Compare this with indexing: password[i] gives a byte, which is one piece of the UTF-8 encoding.
    • The two counts can differ. len(password) is in bytes, while range walks characters. For ASCII passwords they agree. For a password with é, range sees one rune and len counts 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. abcdefgh prints Has digit: False and Weak.
    • A digit but too short. abc1 prints Length: 4, Has digit: True and Weak.
    • Only digits. 12345678 is Strong under these two rules. Nothing here asks for letters.
    • Resetting the flag in the loop. An else that sets hasDigit = false for non-digits would undo an earlier match. With Secret99 the last character is a digit, but with 1abcdefg it would end up false.
    • Printing the boolean directly. Joining hasDigit to a string fails to compile. And fmt.Println(hasDigit) prints true in lowercase, while the expected line says True, 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

    • Secret99 prints Length: 8, Has digit: True and Strong.
    • abcdefgh prints Length: 8, Has digit: False and Weak.
    • abc1 prints Length: 4, Has digit: True and Weak.
    • 12345678 prints Length: 8, Has digit: True and Strong.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free