The real cat command can take many files as arguments and prints them one after another. Build that behavior. Given a list of "files" where each file is itself a list of lines, flatten all lines in order and join them with newlines.
A file can be empty (it adds no lines), and the list can hold no files at all; the result is then an empty string.
Function signature:
func concatCat(allLines [][]string) string
Example:
Input: allLines = [["hello", "world"], ["foo", "bar", "baz"]] Output: "hello\nworld\nfoo\nbar\nbaz" When printed: hello world foo bar baz
Collect every line into a flat
[]string with a nested loop, preserving order. Then join the pieces with "\n".
The
strings package provides strings.Join, so once the lines are flattened you can stitch them together directly:
flat := []string{}
for _, fileLines := range allLines {
for _, line := range fileLines {
flat = append(flat, line)
}
}
return strings.Join(flat, "\n")
The full solution:
func concatCat(allLines [][]string) string {
flat := []string{}
for _, fileLines := range allLines {
for _, line := range fileLines {
flat = append(flat, line)
}
}
return strings.Join(flat, "\n")
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
