Problem overview
Top K Brands is a project built over several versions. It imitates a shopping search page that shows the best-rated brands for a product. Version 1 has the simplest possible data: one product, one brand and one rating.
The starter gives three variables: product := "Laptop", brand := "TechPro" and rating := 4. The program prints the product on the first line and a ranked entry on the second, written as 1. Brand (rating):
Laptop
1. TechPro (4)
Approach: one variable per line
The lesson builds each output line in its own variable, productLine and brandLine, and prints them in one call at the end. It helps to treat the ranked entry as a piece of text assembled from parts: the rank and a dot, the brand, a space, an opening parenthesis, the rating and a closing parenthesis.
The rating is an int, and Go does not allow + between a string and an integer. The rating has to become text first, with strconv.Itoa.
Step by step
- Create
productLineand set it toproduct. The first line is nothing more than the product name, but giving it its own variable keeps the final step simple. - Create
brandLineby joining six pieces with+: the literal"1. ",brand, the literal" (",strconv.Itoa(rating)and the literal")". Mind the spaces inside the literals: one after the dot and one before the parenthesis. - Print both lines with one call. Join
productLine, the newline"\n"andbrandLine, and pass the result tofmt.Println.
The solution in Go
product := "Laptop"
brand := "TechPro"
rating := 4
productLine := product
brandLine := "1. " + brand + " (" + strconv.Itoa(rating) + ")"
fmt.Println(productLine + "\n" + brandLine)
The program needs the fmt and strconv packages.
The details worth knowing
"\n"is one character. Inside a Go string,\nis the newline character. A string that contains it prints on two lines.Printlnthen adds one more newline at the very end, so the output stops after a complete second line.:=declares and infers.productLine := productcreates a newstringvariable; the type comes from the value on the right. Becauserating := 4has no suffix or decimal point, it is anint.- Every variable must be used. Go refuses to compile a local variable that is declared and never read. Each of the five variables here is read, so nothing needs the blank identifier.
strconv.Itoa, notstring(rating).string(rune(65))is"A"because the number is read as a character code, whilestrconv.Itoa(65)is"65". The same mix-up with the rating 4 would give a control character instead of the digit.- The mismatch error.
"1. " + brand + " (" + rating + ")"fails to compile withinvalid operation: "1. " + brand + " (" + rating (mismatched types string and int).
Edge cases and common mistakes
- Missing spaces.
"1."without the trailing space gives1.TechPro (4). The tests compare the text exactly. - Parentheses placed wrongly. The closing parenthesis comes after the rating, and it is part of a string literal, so it needs its own quotes.
- Printing with two calls. Calling
fmt.Printlntwice would print the same two lines. The lesson asks for one joined string. - Adding a trailing newline. Ending the joined string with another
"\n"would add a blank line at the end, becausePrintlnappends a newline of its own.
Testing
- With
product := "Phone",brand := "Nova"andrating := 5, the output isPhoneand then1. Nova (5). - With
rating := 10, the entry ends with(10):strconv.Itoahandles several digits.
