Start learning free

LESSON 68 OF 285 · STRINGS I

Top K Brands V1 in Go: Building Two Lines of Text

Practice Read the full explanation

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

product := "Laptop"
brand := "TechPro"
rating := 4

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (productLine and brandLine), join them with a newline "\n" and print the result with fmt.Println(...).

Go needs strconv.Itoa(rating) to turn the int into text before joining it with +.

productLine + "\n" + brandLine joins both lines into one string.

product := "Laptop"
brand := "TechPro"
rating := 4

productLine := product
brandLine := "1. " + brand + " (" + strconv.Itoa(rating) + ")"
fmt.Println(productLine + "\n" + brandLine)

STEP 1 OF 3
    Ctrl + Enter to check

    GO · STRINGS I

    Top K Brands V1 in Go, explained

    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

    1. Create productLine and set it to product. The first line is nothing more than the product name, but giving it its own variable keeps the final step simple.
    2. Create brandLine by 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.
    3. Print both lines with one call. Join productLine, the newline "\n" and brandLine, and pass the result to fmt.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, \n is the newline character. A string that contains it prints on two lines. Println then adds one more newline at the very end, so the output stops after a complete second line.
    • := declares and infers. productLine := product creates a new string variable; the type comes from the value on the right. Because rating := 4 has no suffix or decimal point, it is an int.
    • 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, not string(rating). string(rune(65)) is "A" because the number is read as a character code, while strconv.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 with invalid operation: "1. " + brand + " (" + rating (mismatched types string and int).

    Edge cases and common mistakes

    • Missing spaces. "1." without the trailing space gives 1.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.Println twice 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, because Println appends a newline of its own.

    Testing

    • With product := "Phone", brand := "Nova" and rating := 5, the output is Phone and then 1. Nova (5).
    • With rating := 10, the entry ends with (10): strconv.Itoa handles several digits.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free