Start learning free

LESSON 195 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Go: Checking Length with len and if/else

Practice Read the full explanation

Check a password's length and print whether it is long enough.

You start with:

password := "Secret99"

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.
  • Use strconv.Itoa(n) to turn a number into text before joining it to a string.

Try other passwords: with abc the output is

Password length: 3
Too short
STEP 1 OF 4
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Go, explained

    Problem overview

    The program starts with password := "Secret99". It prints the password's length on one line and a verdict on the next: OK when the length is at least 8, Too short otherwise. For the starting password:

    Password length: 8
    OK

    With abc the output is Password length: 3 followed by Too short. This is the first version of a password checker, and it applies a single rule. Later versions add more rules.

    Approach: measure once, use the number twice

    The length is needed in two places: printed in the first line and compared with 8 for the verdict. So the program computes it once, stores it in a variable named length, and uses that variable both times.

    The verdict is a two-way choice, so it is an if/else. The condition is length >= 8. It says "at least 8", so a password of exactly 8 characters passes.

    Step by step

    1. Store the length. length := len(password) asks the built-in len function for the size of the string. The type of length is int.
    2. Print the first line. The text "Password length: " is joined to the number. Go does not join an int to a string with +, so strconv.Itoa(length) turns the number into its text first. fmt.Println prints the result and ends the line.
    3. Test the length. An if length >= 8 block prints OK.
    4. Add the else. It prints Too short for every length the if didn't take, which means 7 or fewer. In Go the else must start on the same line as the closing brace of the if: } else {.

    The solution in Go

    password := "Secret99"
    
    length := len(password)
    fmt.Println("Password length: " + strconv.Itoa(length))
    if length >= 8 {
        fmt.Println("OK")
    } else {
        fmt.Println("Too short")
    }
    

    Go details worth knowing here:

    • len counts bytes, not characters. A Go string is a sequence of bytes, and len returns the number of bytes. For plain ASCII text, where each character is one byte, that is the number of characters. For other text it is not: "é" takes two bytes in UTF-8, so len("é") is 2. The string "héllo" has 5 characters but len gives 6. To count characters, use utf8.RuneCountInString(s) from the unicode/utf8 package, or len([]rune(s)); both give 5 for "héllo". The lesson's passwords are ASCII, so len is correct for them.
    • No implicit conversion. "Password length: " + length is a compile error, because the operands are a string and an int. This is deliberate: Go never converts between types behind your back.
    • Braces are required around the bodies of if and else, even for one statement, and the condition needs no parentheses.
    • Imports. The program needs fmt for printing and strconv for Itoa.

    Edge cases and common mistakes

    • Exactly 8 characters. Secret99 passes. Writing length > 8 would reject it. The words "at least" mean greater than or equal.
    • 7 characters. 1234567 prints Password length: 7 and Too short.
    • An empty password. "" has length 0 and prints Too short.
    • Non-ASCII text. "pässwörd" has 8 characters, but len gives 10 because each of ä and ö takes two bytes, so this program prints Password length: 10 and OK. A version that counted characters would say 8.
    • Placing else on a new line. A line break between the closing brace and else is a syntax error in Go.
    • Printing the number without converting it. Passing the int through strconv.Itoa is what makes the join compile.

    Testing

    • Secret99 prints Password length: 8 and OK.
    • abc prints Password length: 3 and Too short.
    • 1234567 prints Password length: 7 and Too short.
    • An empty string prints Password length: 0 and Too short.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free