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
- Store the length.
length := len(password)asks the built-inlenfunction for the size of the string. The type oflengthisint. - Print the first line. The text
"Password length: "is joined to the number. Go does not join anintto a string with+, sostrconv.Itoa(length)turns the number into its text first.fmt.Printlnprints the result and ends the line. - Test the length. An
if length >= 8block printsOK. - Add the
else. It printsToo shortfor every length theifdidn't take, which means 7 or fewer. In Go theelsemust start on the same line as the closing brace of theif:} 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:
lencounts bytes, not characters. A Go string is a sequence of bytes, andlenreturns 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, solen("é")is 2. The string"héllo"has 5 characters butlengives 6. To count characters, useutf8.RuneCountInString(s)from theunicode/utf8package, orlen([]rune(s)); both give 5 for"héllo". The lesson's passwords are ASCII, solenis correct for them.- No implicit conversion.
"Password length: " + lengthis a compile error, because the operands are a string and anint. This is deliberate: Go never converts between types behind your back. - Braces are required around the bodies of
ifandelse, even for one statement, and the condition needs no parentheses. - Imports. The program needs
fmtfor printing andstrconvforItoa.
Edge cases and common mistakes
- Exactly 8 characters.
Secret99passes. Writinglength > 8would reject it. The words "at least" mean greater than or equal. - 7 characters.
1234567printsPassword length: 7andToo short. - An empty password.
""has length 0 and printsToo short. - Non-ASCII text.
"pässwörd"has 8 characters, butlengives 10 because each ofäandötakes two bytes, so this program printsPassword length: 10andOK. A version that counted characters would say 8. - Placing
elseon a new line. A line break between the closing brace andelseis a syntax error in Go. - Printing the number without converting it. Passing the
intthroughstrconv.Itoais what makes the join compile.
Testing
Secret99printsPassword length: 8andOK.abcprintsPassword length: 3andToo short.1234567printsPassword length: 7andToo short.- An empty string prints
Password length: 0andToo short.
