Problem overview
Version 2 adds the -n flag of the cat tool, as a new function: numberedCat(lines []string) string. Each line is preceded by its 1-based line number, right-aligned in a field 6 characters wide, then a tab, then the line text. Lines are still joined with newlines, with none after the last one. Blank lines are numbered too, and an empty slice gives an empty string.
The previous version already joined lines with \n. This one keeps that loop and changes what is appended for each line.
Approach: format each row, keep the separator rule
The loop is the same as before: add a newline before every row except the first. The new work is the row itself, which is made of three parts: the number padded to 6 characters, a tab, and the original line. fmt.Sprintf builds such a string from a pattern, and the verb %6d prints an integer right-aligned in 6 columns, padded with spaces on the left.
The index i starts at 0, but lines are numbered from 1, so the number passed in is i+1.
Step by step
- Start with an empty
resultstring. - Loop with
ifrom 0 whilei < len(lines). The index is used twice: for the number and to pick the line. - Add a newline when
i > 0, so rows are separated and none ends the result. - Append the formatted row with
fmt.Sprintf("%6d\t%s", i+1, lines[i]). The values follow the pattern in order: the integer for%6d, the string for%s. - Return
resultafter the loop.
The solution in Go
func numberedCat(lines []string) string {
result := ""
for i := 0; i < len(lines); i++ {
if i > 0 {
result += "\n"
}
result += fmt.Sprintf("%6d\t%s", i+1, lines[i])
}
return result
}
The Go details:
Sprintfreturns a string. UnlikePrintf, which writes to the output,Sprintfhands back the formatted text, so it can be appended toresult. The function needs thefmtimport.\tinside the pattern is a real tab. The double-quoted string literal turns the two characters\tinto one tab character beforeSprintfsees it.%6dpads, it doesn't cut. The width is a minimum. A number with more than 6 digits is printed in full, which pushes that row out of alignment:fmt.Sprintf("%6d\t%s", 1234567, "x")gives"1234567\tx".- Arguments must match the verbs. Passing a string where
%dexpects an integer doesn't stop the build. The output contains an error marker such as%!d(string=...)instead of a number, so check the order of the arguments.
Tracing
These results come from running the function (shown as Go string literals):
numberedCat(["hello", "world", "foo"])
-> " 1\thello\n 2\tworld\n 3\tfoo"
numberedCat(nil) -> ""
Printing the result of numberedCat([]string{"hello", "", "foo"}) shows the blank line numbered. The tab character is the gap between the number and the text:
1 hello
2
3 foo
With 1000 identical lines, the end of the output is "999\tx\n 1000\tx": the wider numbers take more of the 6 columns, so the padding shrinks.
Edge cases and common mistakes
- Starting at 0. Passing
iinstead ofi+1makes the first line number 0. - Skipping blank lines. They are numbered like any other line, and a blank line still gets its number and tab.
- The wrong width.
%5dor%-6dchanges the alignment: the minus sign left-aligns the number and puts the padding after it. - Spaces in place of the tab. The separator is one tab character, not several spaces.
- A trailing newline. As in the first version, a newline goes between rows only.
